我又又又報(bào)錯(cuò)了...
<?php?
/**
?* param string $fileName
?* param string $maxSize
?* param string uploadPath
?* param array $allowMime
?* param array $endExt
?* param array $fileInfo
?*/
//面向?qū)ο髮懸粋€(gè)文件上傳
class upload{
protected $fileName;
protected $maxSize;
protected $endExt;
protected $uploadPath;
protected $flage;
protected $allowMime;
protected $fileInfo;
protected $error;
protected $ext;
protected $destination;
protected $uniqid;
//構(gòu)造函數(shù),自己上傳的數(shù)據(jù)
public function __construct($fileName='myfile',
$maxSize=5242880,//5M
$endExt=array('jpg','jpeg','png','gif'),
$uploadPath='./uploads',
$flage=true,
$allowMime=array('image/jpg','image/jpeg','image/png','image/gif')
){
$this->fileName=$fileName;//瀏覽框名稱
$this->maxSize=$maxSize;//服務(wù)器端大小限制
$this->endExt=$endExt;//限制用戶只能在自己設(shè)置的后綴名稱內(nèi)上傳指定類型文件
$this->uploadPath=$uploadPath;//上傳到指定m目錄
$this->flage=$flage;//是否檢測(cè)文件類型
$this->allowMime=$allowMime;//通過文件類型來判斷該文件是否是惡意修改后綴來獲得上傳
$this->fileInfo=$_FILES[$this->fileName];//上傳文件的信息保存在變量fileInfo中
/*接收到上傳的信息$_FILES數(shù)組中自己要接受的fileName的信息*/
}
//判斷錯(cuò)誤號(hào),由于在對(duì)象內(nèi)使用,設(shè)置為私有
protected function checkERROR(){
if ($this->fileInfo['error']>0) {
switch ($fileInfo['error']) {
case 1:
$this->error='有錯(cuò)誤發(fā)生! UPLOAD_ERR_INI_SIZE :其值為1,上傳的文件超過了php.ini中的upload_max_filesize選項(xiàng)中限制的值';
break;
case 2:
$this->error='有錯(cuò)誤發(fā)生! UPLOAD_ERR_FORM_SIZE
:其值為2,上傳的文件大小超過了HTML表單中max_file_size限制的值';
break;
case 3:
$this->error='有錯(cuò)誤發(fā)生! UPLOAD_ERR_PARTIAL:其值為3,部分文件未上傳';
break;
case 4:
$this->error='有錯(cuò)誤發(fā)生! UPLOAD_ERR_NO_FILE:其值為4,沒有文件被上傳';
break;
case 6:?
$this->error='有錯(cuò)誤發(fā)生! UPLOAD_ERR_NO_TMP_DIR:其值為6,找不到臨時(shí)文件夾';
break;
case 7:
$this->error='有錯(cuò)誤發(fā)生! UPLOAD_ERR_CANT_WRITE:其值為7,文件寫入失敗';
break;
case 8:
$this->error='有錯(cuò)誤發(fā)生! UPLOAD_ERR_EXTENSION:其值為8,上傳的文件被PHP擴(kuò)展程序中斷';
break;
}
return flase;
}
return true;
}
// 判斷文件大小是否符合規(guī)定
protected function checkMax(){
if ($this->maxSize<$this->fileInfo['size']) {
$this->error='上傳文件過大';
return flase;
}
return true;
}
//檢測(cè)擴(kuò)展名是否在允許的范圍內(nèi)
protected function checkExt(){
//用到了$ext,就設(shè)置一個(gè)$ext屬性
$this->ext=strtolower(pathinfo($this->fileinfo['name'],PATHINFO_EXTENSION));
if (!is_array($this->ext,$this->endExt)) {
$this->error='擴(kuò)展名不符合要求';
return flase;
}
return true;
}
//檢測(cè)上傳方式是否為post
protected function checkHttpPost(){
if (!is_uploaded_file($this->fileInfo['tmp_name'])) {
$this->error='上傳方式不符合要求';
return flase;
}
return true;
}
//檢測(cè)文件類型
protected function checkMime(){
if (!is_array($this->allowMime,$this->endExt){
$this->error='文件類型MIME非法';
return flase;
}
return true;
}
//檢測(cè)是否為一個(gè)真實(shí)的圖片
protected function checekimage(){
if ($this->flage) {
if (!getimagesize($this->fileInfo['tmp_name'])) {
$this->error='文件不是一個(gè)真實(shí)的圖片';
return flase;
}
return ture;
}
}
//顯示錯(cuò)誤
protected function ShowError(){
exit('<span style:"color=red"><strong>'.$this->error.'</strong></span>');
}
//判斷是否目錄名是否存在
protected function File(){
if (!file_exists($this->uploadPath)) {
mkdir($uploadPath,0777,true);
chmod($uploadPath,0777);
}
}
//上傳文件
public function uploadfile(){
if ($this->checkMax()&&$this->checkHttpPost()&&$this->checkMime()&&$this->checkExt()&&$this->checkERROR()&&checekimage()){
$this->uniqid=$this->setuniqid();
$this->destination=$this->uploadPath.'/'.$this->uniqid.'.'.$this->ext;
//移動(dòng)文件
if (@move_uploaded_file($this->fileInfo['tmp_name'],$this->destination)) {
return $this->destination;
}else{
$this->error='文件移動(dòng)失??!';
$this->ShowError();
}
}else{
$this->ShowError();
}
}
}
??>
2021-04-28
頂不住了?你這個(gè)BUG比我的還多?我還想復(fù)制粘貼省事然后整到一半重新自己寫了
2019-09-05
is_array改成in_array試試行不行
2019-05-06
第107行上下的代碼在這里...