本文共 25428 字,大约阅读时间需要 84 分钟。
//可在提交表单时将action设置为另一个单独的php文件,并通过一些值的设定,如上传路径,是否使用随机文件名,允许格式来初始化类中所定义的私有变量。以下代码中的pic为上传文件表单input中的name值。 require("FileUpload.class.php"); $up=new UploadFile(array('filepath' => './upload/', 'MAXSIZE' => 2000000, 'isRandName' => true, 'allowtype' => array('jpg', 'gif'))); if($up->uploadFile('pic')){ echo $up->getNewFileName(); }else{ echo $up->getErrorMsg(); }if(!$return){ $this->errorMess = $this->getError(); } return $return; } private function copyFile(){ if(!$this->errorNum){ $filepath=rtrim($this->filepath, '/').'/'; $filepath .= $this->newFileName; if(@move_uploaded_file($this->tmpFileName, $filepath)){ return true; }else{ $this->setOption('errorNum', -3); return false; } }else{ return false; } } //设置与$_FILES有关的内容 private function setFiles($name="",$tmp_name="",$size=0,$error=0){ $this->setOption('errorNum', $error); if($error){ return false; } $this->setOption('originName', $name); $this->setOption('tmpFileName', $tmp_name); $arrStr = explode('.', $name); $this->setOption('fileType', strtolower($arrStr[count($arrStr)-1])); $this->setOption('fileSize', $size); return true; } //用于获取上传后文件的文件名 function getNewFileName(){ return $this->newFileName; } //上传如果失败,则调用这个方法查看错误报告 function getErrorMsg(){ return $this->errorMess; } }以下是兄弟连PHP视频教程中高洛峰老师所教授的文件上传类源代码,记录以备后续参考:
$value){ $key=strtolower($key); if(!in_array($key, get_class_vars(get_class($this)))){ continue; } $this->setOption($key, $value); } } /*function __construct($filepath, $allowtype, $maxsize, $israndname){ $this->filepath=$filepath; $this->allowtype=$allowtype; $this->maxsize=$maxsize; $this->israndname=$israndname; }*/ private function getError(){ $str="上传文件{$this->originName}时出错:"; switch ($this->errorNum) { case 4: $str.="没有文件补上传"; break; case 3: $str __("文件只被部分上传"); break; case 2: $str __("上传文件超过了html表单中MAX_FILE_SIZE中设定的值"); break; case 1: $str __("上传文件超过了php.ini中upload_max_filesize选项的值"); break; case -1: $str __("未允许的类型"); break; case -2: $str __("文件过大,上传不能超过{$this->maxsize}个字节"); break; case -3: $str __("上传失败"); break; case -4: $str __("建立存放上传文件目录失败,请重新指定上传目录"); break; case -5: $str __("必须指定上传文件的路径"); break; default: $str __("未知错误"); break; } return $str." } private function checkFilePath(){ if(empty($this->filepath)){ $this->setOption('errorNum', -5); return false; } if(!file_exists($this->filepath) || !is_writable($this->filepath)){ if(!@mkdir($this->filepath, 0755)){ $this->setOption('errorNum', -4); return false; } return true; } private function checkFileSize(){ if($this->fileSize > $this->maxsize){ $this->setOption('errorNum', -2); return false; }else{ return true; } private function checkFileType(){ if(in_array(strtolower($this->fileType), $this->allowtype)){ return true; }else{ $this->setOption('errorNum', -1); return false; } private function setNewFileName(){ if($this->israndname){ $this->setOption('newFileName', $this->proRandName()); }else{ $this->setOption('newFileName', $this->originName); } private function proRandName(){ $fileName=date("YmdHis").rand(100,999); return $fileName.'.'.$this->fileType; } private function setOption($key, $value){ $this->$key = $value; } //用于上传文件 function uploadFile($fileField){ $return=true; //检查文件上传路径 if(!$this->checkFilePath()){ $this->errorMess = $this->getError(); return false; } $name=$_FILES[$fileField]['name']; $tmp_name=$_FILES[$fileField]['tmp_name']; $size=$_FILES[$fileField]['size']; $error=$_FILES[$fileField]['error']; //判断是否上传多个文件 if(is_array($name)){ $errors=array(); for($i=0; issetFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i])){ if(!$this->checkFileSize() || !$this->checkFileType()){ $errors[] = $this->getError(); return false; } }else{ $errors[] = $this->getError(); return false; } }else{ $errors[] = $this->getError(); return false; } if(!$return){ $this->setFiles(); //错误时初始化 } } if($return){ $fileNames=array(); for($i=0; issetFiles($name[$i], $tmp_name[$i], $size[$i], $error[$i])){ $this->setNewFileName(); if(!$this->copyFile()){ $errors = $this->getError(); $return=false; }else{ $fileNames[] = $this->newFileName; } } } }else{ if($this->setFiles($name, $tmp_name, $size, $error)){ if($this->checkFileSize() && $this->checkFileType()){ $this->setNewFileName(); if($this->copyFile()){ return true; }else{ return false; } }else{ $return=false; } }else{ $return=false; }
转载地址:http://futfk.baihongyu.com/