diff --git a/application/module/Westdc/Dataimport/Upload.php b/application/module/Westdc/Dataimport/File.php similarity index 51% rename from application/module/Westdc/Dataimport/Upload.php rename to application/module/Westdc/Dataimport/File.php index 4d2eec60..c86740ae 100644 --- a/application/module/Westdc/Dataimport/Upload.php +++ b/application/module/Westdc/Dataimport/File.php @@ -1,7 +1,7 @@ config = \Zend_Registry::get('config'); } - - //上传數據 - public function __invoke($file) + + /** + * 獲取文件存儲目錄 + * @return string + */ + private function getSavePath() + { + if(!preg_match("/(\\/|\\\)$/",$this->config->dataimport->path)) + return $this->config->dataimport->path . DIRECTORY_SEPARATOR; + else + return $this->config->dataimport->path; + } + + /** + * 上傳文件 + * @param $file + * @return array + */ + public function upload($file) { try{ if (empty($file) !== false) { @@ -29,13 +45,8 @@ class Upload{ $ext = $this->getFileTextExt($file['name']); $filename = $this->gen_uuid().".".$ext; - - if(!preg_match("/(\\/|\\\)$/",$this->config->dataimport->path)) - $save_path = $this->config->dataimport->path . DIRECTORY_SEPARATOR; - else - $save_path = $this->config->dataimport->path; - $new_filepath = $save_path.$filename; + $new_filepath = $this->getSavePath().$filename; if (move_uploaded_file($file['tmp_name'], $new_filepath) === false) { return array("error"=>"上传失败,请重试"); @@ -43,16 +54,20 @@ class Upload{ return array( "success" => 1, - "file"=>$new_filepath + "file"=>$filename ); }catch(Exception $e) { return array("error"=>$e->getMessage()); } - } - - //获取文件扩展名 + }//upload() + + /** + * 获取文件扩展名 + * @param $file_name + * @return mixed|string + */ public function getFileTextExt($file_name) { $temp_arr = explode(".", $file_name); @@ -61,8 +76,12 @@ class Upload{ $file_ext = strtolower($file_ext); return $file_ext; } - - //获取文件Mime,通过finfo扩展 + + /** + * 獲取文件Mime + * @param $file_name + * @return mixed + */ public function getFileMime($file_name) { $finfo = finfo_open(FILEINFO_MIME_TYPE); @@ -70,8 +89,11 @@ class Upload{ finfo_close($finfo); return $filetype; } - - //文件名uuid + + /** + * 生成文件名UUID + * @return string + */ public function gen_uuid() { return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x', // 32 bits for "time_low" @@ -94,4 +116,64 @@ class Upload{ ); } + /** + * 獲得已經上傳的文件 + * @return array + */ + public function getUploadFiles() + { + return $this->fetchDir($this->getSavePath()); + } + + /** + * 掃描文件夾 + * @param $directory + * @return array + */ + public function fetchDir($directory){ + $mydir = dir($directory); + + $files = []; + + while($file = $mydir->read()) + { + if($file == '.' || $file == "..") + continue; + + if((is_dir("$directory/$file")) AND ($file!=".") AND ($file!="..")) + { + $files = array_merge($files,$this->fetchDir("$directory/$file")); + } + else + $files[] = $file; + } + + $mydir->close(); + + return $files; + } + + /** + * 刪除文件 + * @param $file + * @return bool + */ + public function deleteFile($file) + { + if(empty($file)) + return false; + + return unlink($this->getSavePath() . $file); + } + + /** + * 獲取文件真實路徑 + * @param $file + * @return string + */ + public function getRealName($file) + { + return $this->getSavePath().$file; + } + } \ No newline at end of file diff --git a/application/module/Westdc/Dataimport/ProcessFactory.php b/application/module/Westdc/Dataimport/ProcessFactory.php new file mode 100644 index 00000000..3eefc922 --- /dev/null +++ b/application/module/Westdc/Dataimport/ProcessFactory.php @@ -0,0 +1,39 @@ +source_file = $file; + $this->csv_object = Reader::createFromPath($this->source_file); + $this->csv_object->setDelimiter(','); + $this->csv_object->setEncodingFrom("utf-8"); + } + + /** + * 獲取總行數 + * @return int + */ + public function getLineCount(){ + return count($this->csv_object->fetchAll()); + }//getLineCount() + + /** + * 獲取文件大小 + * @return int + */ + public function getSize(){ + + $size = filesize($this->source_file); + + $type = array('Bytes','KB','MB','GB','TB'); + for($i = 0; $size >= 1024; $i++)//单位每增大1024,则单位数组向后移动一位表示相应的单位 + { + $size/=1024; + } + return (floor($size*100)/100).$type[$i];//floor是取整函数,为了防止出现一串的小数,这里取了两位小数 + }//getSize() + + /** + * 獲取文件類型 + * @return mixed + */ + public function getType(){ + $finfo = finfo_open(FILEINFO_MIME_TYPE); + $filetype = finfo_file($finfo, $this->source_file) ; //文件mime类型 + finfo_close($finfo); + return $filetype; + }//getType() + + /** + * 獲取內容 + * @return mixed + */ + public function getLines() + { + return $this->csv_object->fetchAll(); + }//getLines() + + /** + * @return mixed + */ + public function getHtmlTable() + { + return $this->csv_object->toHTML('table-csv-data with-header'); + }//getTable() + + /** + * 檢查文件內容是否規則 + * @return array|bool + */ + public function checkRegularity() + { + $lines = $this->getLines(); + + $error_stack = []; + + foreach($lines as $k=>$v){ + if($k < 1 || empty($v)) + continue; + + if(count($v) != count($lines[$k-1])) + { + $error_stack[] = self::ERROR_LINE_TITLE . ($k + 1) .":".self::ERROR_INDEX_NOT_MATCHED; + continue; + } + + foreach($v as $vIndex => $item){ + if(gettype($item) != gettype($lines[$k-1][$vIndex])) + { + $error_stack[] = self::ERROR_LINE_TITLE . ($k + 1) .":".self::ERROR_INDEX_TYPE_DIFFERENT; + } + } + } + + if(count($error_stack) < 1) + return true; + else + return $error_stack; + + }//checkRegularity() + + + +} \ No newline at end of file diff --git a/application/module/Westdc/Dataimport/ProcessingInterface.php b/application/module/Westdc/Dataimport/ProcessingInterface.php new file mode 100644 index 00000000..ac43765d --- /dev/null +++ b/application/module/Westdc/Dataimport/ProcessingInterface.php @@ -0,0 +1,19 @@ +