接收不到$_FILES数组
来源:4-3 文件上传类封装(三)
习惯牵你手
2019-10-07 11:30:25
<?php
class UploadFile
{
const UPLOAD_ERROR = [
UPLOAD_ERR_INI_SIZE => '文件大小超出php.ini当中的upload_max_filesizes的大小',
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扩展阻止',
];
protected $field_name;
protected $destination_dir;
protected $allow_mime;
protected $allow_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;
public function __construct($keyName,$destinationDir = './upload',$allowMime = ['image/jpeg','image/png','image/gif'],$allowExt = ['jpeg','jpg','png','gif'],$allowSize = 2*1024*1024){
$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;
}
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();
//检查扩展名
$this->checkExt();
//检查文件大小
$this->checkSize();
//生成新的文件名称
$this->generateNewName();
//移动文件
$this->moveFile();
if(count($this->getError())>0){
return false;
}
return true;
}
public function getError(){
return $this->errors;
}
protected function setFileInfo(){
$this->file_org_name = $_FILES[$this->field_name]['name'];
$this->file_type = $_FILES[$this->field_name]['type'];
$this->file_tmp_name = $_FILES[$this->field_name]['tmp_name'];
$this->file_error = $_FILES[$this->field_name]['error'];
$this->file_size = $_FILES[$this->field_name]['size'];
}
protected function setError($error){
$this->errors[] = $error;
}
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;
}
protected function checkMime(){
if(!in_array($this->file_type,$this->allow_mime)){
$this->setError( '文件类型'.$this->file_type.'不被允许');
return false;
}
return true;
}
protected function checkExt(){
$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().'.'.$this->extension;
}
protected function moveFile(){
if(!file_exists($this->destination_dir)){
mkdir($this->destination_dir,0777,true);
}
$newName = $this->destination_dir.'/'.$this->file_new_name;
if(is_uploaded_file($this->file_tmp_name) && move_uploaded_file($this->file_tmp_name, $newName)){
return true;
}
$this->setError('上传失败');
return FALSE;
}
public function getFileName(){
return $this->file_new_name;
}
public function getDestinationDir(){
return $this->destination_dir;
}
public function getExtension(){
return $this->destination_dir;
}
public function getFileSize(){
return $this->file_size;
}
}
?>
<?php
require('UploadFile.php');
$upload = new UploadFile('file',['image/jpeg','image/png','image/gif'],['jpeg','jpg','png','gif'],2*1024*1024);
$upload->setDestinationDir('./upload');
$upload->setAllowMime(['image/jpeg','image/png','image/gif']);
$upload->setAllowExt(['jpeg','jpg','png','gif']);
$upload->setAllowSize(2*1024*1024);
if($upload->upload()){
echo $newName = $upload->getFileName().'<br>';
echo $destDir = $upload->getDestinationDir().'<br>';
echo $ext = $upload->getExtension().'<br>';
echo $size = $upload->getFileSize().'<br>';
}else{
var_dump($upload->getErrors());
}
?>
1回答
您好, 1.var_dump($upload->getErrors());中getErrors方法不存在,应该是getError()。
2.__construct方法中$this->filed_name = $keyName;的filed_name名称错误,应该是field_name。
3.下图第一个参数是要和.html文件中name值相同。
祝学习愉快!
相似问题