文件上传错误问题
来源:4-3 文件上传类封装(三)
慕运维1594908
2019-08-09 11:12:02
php 代码 header('Content-Type:text/html;charset=utf-8'); class UploadFile{ protected $filed_name; protected $destination_dir; protected $allow_mime; protected $all_ext; protected $allow_size; protected $file_org_name; protected $file_type; protected $file_tmp_name; protected $file_error; protected $file_size; protected $errors=[]; protected $extension; protected $file_new_name; const UPLOAD_ERROR = [ UPLOAD_ERR_INI_SIZE => '文件大小超出了php.ini当中的upload_max_filesize的值', UPLOAD_ERR_FORM_SIZE => '文件大小超出了MAX_FILE_SIZE的值', UPLOAD_ERR_PARTIAL => '文件只有部分被上传', UPLOAD_ERR_NO_FILE => '没有文件被上传', UPLOAD_ERR_NO_TMP_DIR => '找不到临时目录', UPLOAD_ERR_CANT_WRITE => '写入磁盘失败', UPLOAD_ERR_EXTENSION => '文件上传被PHP扩展阻止' ]; //构造方法初始化 public function __construct($keyName,$destinationDir='./upload',$allowMime=array('image/jpeg','image/jpg'),$allowExt=array('jpg','jpeg'),$allowSize=1024*1024*2) {$this->filed_name=$keyName; $this->destination_dir=$destinationDir; $this->allow_mime=$allowMime; $this->allow_ext=$allowExt; $this->allow_size=$allowSize; } //设置上传文件的指定目录 public function setDestinationDir($destinationDir){ $this->destination_dir = $destinationDir; } //设置上传文件允许的mime类型 public function setAllowMime($allowMime){ $this->allow_mime = $allowMime; } //设置允许文件的扩展名 public function setAllowExt($allowExt){ $this->allow_ext = $allowExt; } //设置上传文件的大小 public function setAllowSize($allowSize){ $this->allow_size = $allowSize; } //上传文件 public function upload(){ //接收$_FILES上传过来的参数 $this->setFileInfo(); //判断错误类型 $this->checkError(); //检查文件的mime $this->checkMime(); //检查文件的exc $this->checkExc(); //检查文件的大小 $this->checkSize(); //给文件一个新的名字 $this->generateNewName(); //移动文件到指定目录 $this->moveFile(); if(count($this->getError())>0){ return false; } return true; } //文件信息 protected function setFileInfo(){ $this->file_org_name = $_FILES[$this->filed_name]['name']; $this->file_type=$_FILES[$this->filed_name]['type']; $this->file_tmp_name=$_FILES[$this->filed_name]['tmp_name']; $this->file_error = $_FILES[$this->filed_name]['error']; $this->file_size= $_FILES[$this->filed_name]['size']; } //错误信息 protected function setError($error){ $this->errors[]=$error; } //取得错误信息 public function getError(){ return $this->errors; } //判断错误类型 protected function checkError(){ if($this->file_error>UPLOAD_ERR_OK){ switch ($this->file_error) { case UPLOAD_ERR_INI_SIZE: case UPLOAD_ERR_FORM_SIZE: case UPLOAD_ERR_PARTIAL: case UPLOAD_ERR_NO_FILE: case UPLOAD_ERR_NO_TMP_DIR: case UPLOAD_ERR_CANT_WRITE: case UPLOAD_ERR_EXTENSION: $this->setError(self::UPLOAD_ERROR[$this->file_error]); return false; } } return true; } //检测文件的mime类型 protected function checkMime(){ if(!in_array($this->file_type,$this->allow_mime)){ $this->setError("文件类型".$this->file_type."不允许上传"); return false; } return true; } //检测文件的扩展名 protected function checkExc(){; $this->extension = pathinfo($this->file_org_name,PATHINFO_EXTENSION); if(!in_array($this->extension,$this->allow_ext)){ $this->setError("文件扩展名".$this->extension."不允许上传"); return false; } return true; } //检测文件的大小 protected function checkSize(){ if($this->file_size>$this->allow_size){ $this->setError("文件的大小".$this->file_size."超出了允许上传的大小".$this->allow_size); return false; } return true; } //生成新的文件名 protected function generateNewName(){ $this->file_new_name=uniqid(microtime(true),true).'.'.$this->extension; } //移动文件名字到指定目录 protected function moveFile(){ if(!file_exists($this->destination_dir)){ mkdir($this->destination_dir,0777,true); } $newDir = rtrim($this->destination_dir,'/').'/'.$this->file_new_name; if((count($this->errors)==0)&&is_uploaded_file($this->file_tmp_name)){ move_uploaded_file($this->file_tmp_name, $newDir); return true; } return false; } //获取文件名称 public function getFileName(){ return $this->file_new_name; } //获取文件扩展名 public function getExtension() { return $this->extension; } //获取文件大小 public function getFileSize() { return $this->file_size; } //获取文件目录名 public function getDestinationDir() { return $this->destination_dir; } }
html代码
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8">
</head>
<body>
<form action="uploadFiles3.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1024000">
<input type="file" name="upload_file">
<input type="submit" value="上传">
</body>
</html>
uploadFiles3.php代码
<?php
require('upload.class.php');
$upload = new UploadFile('upload_file');
$upload->upload();
if ($upload->upload()) {
echo"文件上传成功"."<br/>";
var_dump($upload->getFileName());
echo "<br />";
var_dump($upload->getDestinationDir());
echo "<br />";
var_dump($upload->getExtension());
echo "<br />";
var_dump($upload->getFileSize());
echo "<br />";
} else {
var_dump($upload->getError());
}
上传错误文件错误信息是双份
array(4) {
[0]=>
string(42) "文件类型text/javascript不允许上传"
[1]=>
string(32) "文件扩展名js不允许上传"
[2]=>
string(42) "文件类型text/javascript不允许上传"
[3]=>
string(32) "文件扩展名js不允许上传"
}
双份的问题在哪
1回答
您好,1.html代码中缺少</form>标签。
2.uploadFiles3.php代码中写了两次$upload->upload(),会认为调用了两次,所以最后输出的结果是双份。将if判断之前的$upload->upload()注释掉即可。
祝学习愉快!
相似问题