/** * Class UploadFile * @author fengzi */ class UploadFile { public $error = array(); //上传前的error信息 public $info = array(); //上传过程中的错误信息 protected $uploadType; //标识是单文件还是多文件上传 protected $files; //上传的文件 protected $path; //上传文件的新路径 public $maxsize = 8 * 1024 * 1024; //设置上传文件的大小 public $allow_type = array('gif', 'jpg', 'png', 'zip', 'rar', 'txt', 'doc', 'pdf','docx', 'jpeg', 'pptx', 'mp4', 'avi', 'mpeg', 'xls', 'xlsx'); //默认允许用户上传的文件类型 public $save_info = array(); //上传成功后,返回一组有用信息 function __construct($files, $path, $size=0){ $this->path = $path; $this->uploadType = $this->singleOrManyFile($files); $this->maxsize = empty($size) ? 8 * 1024 * 1024 : $size * 1024 * 1024; if ( !$this->isError($files, $this->uploadType) ) { return $this->error; } $this->isPathExist(); } //上传文件 public function upload() { $num = count($this->files['name']); for ($i=0; $i<$num; $i++) { $name = $this->files['name'][$i]; $type = $this->files['type'][$i]; $tmp_name = $this->files['tmp_name'][$i]; $size = $this->files['tmp_name'][$i]; //判断上传文件有没有超过最大值 if ( !$this->checkSize($size) ) { $this->setInfo($name, 'size'); continue; } //获得随即文件名 $newFileName = $this->proRandName($name); $new_path = $this->path.'/'.$newFileName; //判断上传类型是否正确 $ext = trim($this->getFileExt($name)); if ( !$this->checkFileType($ext) ) { $this->setInfo($name, 'ext', $ext); continue; } //判断文件是否是通过 HTTP POST 上传的 if( !is_uploaded_file($tmp_name) ) { $this->setInfo($name, 'tmp_name'); continue; } //上传文件 if( !@move_uploaded_file($tmp_name, $new_path) ) { $this->setInfo($name, 'move_uploaded_file'); continue; } else { //存储当前文件的有关信息,以便其它程序调用 $this->save_info[] = array( "name" => $name, "new_name" => $newFileName, "ext" => $ext, "mime_type" => $type, "size" => $size, "save_path" => $this->path, ); } } } //获取上传成功后的信息 public function getSaveInfo() { return $this->save_info; } //获取上传中的错误信息 public function getInfo() { return $this->info; } //获取上传时的错误信息 public function getError() { return $this->error; } //记录上传过程中的错误 private function setInfo($name, $flag, $ext='') { switch ($flag) { case 'size': $info = "上传文件超过了设置的最大值"; break; case 'ext': $str = implode(',', $this->allow_type); $info = "上传的文件类型{$ext}不在{$str}中"; break; case 'tmp_name': $info = "文件不是通过 HTTP POST 上传的"; break; case 'move_uploaded_file': $info = "上传文件失败"; break; } $array = array( 'filename' => $name, 'info' => $info, ); $this->info[] = $array; } //检查保存目录是否存在 private function isPathExist() { if ( !file_exists($this->path) || !is_writable($this->path) ) { @mkdir($this->path); chmod($this->path, 0777); } } //判断是单文件还是多文件上传 private function singleOrManyFile($files) { if ( is_array($files['name']) ) { return 'many'; } else { return 'single'; } } //判断上传文件是否有错误 private function isError($files, $uploadType) { if ( $uploadType == 'single' ) { $this->files = array(); $this->files['name'] = array($files['name']); $this->files['type'] = array($files['type']); $this->files['tmp_name'] = array($files['tmp_name']); $this->files['error'] = array($files['error']); $this->files['size'] = array($files['size']); } else { $this->files = $files; } $num = count($this->files['name']); for ($i=0; $i<$num; $i++) { if ( $this->files['error'][$i] > 0 ) { switch ($this->files['error'][$i]) { case 1: $this->error[] = '文件大小超过了PHP.ini中的文件限制'; break; case 2: $this->error[] = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值'; break; case 3: $this->error[] = '文件部分被上传'; break; case 4: $this->error[] = '没有文件被上传'; break; case 5: $this->error[] = '服务器临时文件夹丢失'; break; case 6: $this->error[] = '文件写入到临时文件夹出错'; break; case 7: $this->error[] = '文件写入失败'; break; case -1: $this->error[] = '末充许的类型'; break; case -2: $this->error[] = '文件过大,上传的文件不能超过'.$this->maxsize.'个字节'; break; case -3: $this->error[] = '上传失败'; break; case -4: $this->error[] = '建立存放上传文件目录失败,请重新指定上传目录'; break; case -5: $this->error[] = '必须指定上传文件的路径'; break; } } else if( $this->files['size'][$i] > $this->maxsize) { $this->error[] = '上传文件的大小超过了 HTML 表单中 MAX_FILE_SIZE 选项指定的值'; } } if ( !empty($this->error) ) { return false; } else { return true; } } //判断上传文件是否为允许类型 private function checkFileType($ext) { if ( in_array($ext, $this->allow_type) ) { return true; } else { return false; } } //设置随机文件名 private function proRandName($name) { $fileName = date('YmdHis')."_".uniqid().mt_rand(100000,999999); //获取随机文件名 return $fileName.'.'.$this->getFileExt($name); //返回文件名加原扩展名 } //获取文件后缀 private function getFileExt($name) { $point = strripos(strtolower($name), '.'); $ext = substr($name, $point+1); return $ext; } //检测用户提交文件大小是否合法 private function checkSize($size) { if ($size > $this->maxsize) { return false; } else { return true; } } }
标签:files,name,break,本地,error,array,php,上传 From: https://www.cnblogs.com/mklblog/p/16620707.html