亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定

我又又又報錯了...

<?php?


/**

?* param string $fileName

?* param string $maxSize

?* param string uploadPath

?* param array $allowMime

?* param array $endExt

?* param array $fileInfo

?*/

//面向對象寫一個文件上傳

class upload{

protected $fileName;

protected $maxSize;

protected $endExt;

protected $uploadPath;

protected $flage;

protected $allowMime;

protected $fileInfo;

protected $error;

protected $ext;

protected $destination;

protected $uniqid;


//構造函數,自己上傳的數據

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;//服務器端大小限制

$this->endExt=$endExt;//限制用戶只能在自己設置的后綴名稱內上傳指定類型文件

$this->uploadPath=$uploadPath;//上傳到指定m目錄

$this->flage=$flage;//是否檢測文件類型

$this->allowMime=$allowMime;//通過文件類型來判斷該文件是否是惡意修改后綴來獲得上傳

$this->fileInfo=$_FILES[$this->fileName];//上傳文件的信息保存在變量fileInfo中

/*接收到上傳的信息$_FILES數組中自己要接受的fileName的信息*/

}

//判斷錯誤號,由于在對象內使用,設置為私有

protected function checkERROR(){

if ($this->fileInfo['error']>0) {

switch ($fileInfo['error']) {

case 1:

$this->error='有錯誤發生! UPLOAD_ERR_INI_SIZE :其值為1,上傳的文件超過了php.ini中的upload_max_filesize選項中限制的值';

break;

case 2:

$this->error='有錯誤發生! UPLOAD_ERR_FORM_SIZE

:其值為2,上傳的文件大小超過了HTML表單中max_file_size限制的值';

break;


case 3:

$this->error='有錯誤發生! UPLOAD_ERR_PARTIAL:其值為3,部分文件未上傳';

break;


case 4:

$this->error='有錯誤發生! UPLOAD_ERR_NO_FILE:其值為4,沒有文件被上傳';

break;


case 6:?

https://img1.sycdn.imooc.com//5cd04b9400013a5a12570242.jpg

$this->error='有錯誤發生! UPLOAD_ERR_NO_TMP_DIR:其值為6,找不到臨時文件夾';

break;


case 7:

$this->error='有錯誤發生! UPLOAD_ERR_CANT_WRITE:其值為7,文件寫入失敗';

break;

case 8:

$this->error='有錯誤發生! UPLOAD_ERR_EXTENSION:其值為8,上傳的文件被PHP擴展程序中斷';

break;

}

return flase;

}

return true;

}

// 判斷文件大小是否符合規定

protected function checkMax(){

if ($this->maxSize<$this->fileInfo['size']) {

$this->error='上傳文件過大';

return flase;

}

return true;

}

//檢測擴展名是否在允許的范圍內

protected function checkExt(){

//用到了$ext,就設置一個$ext屬性

$this->ext=strtolower(pathinfo($this->fileinfo['name'],PATHINFO_EXTENSION));

if (!is_array($this->ext,$this->endExt)) {

$this->error='擴展名不符合要求';

return flase;

}

return true;

}

//檢測上傳方式是否為post

protected function checkHttpPost(){

if (!is_uploaded_file($this->fileInfo['tmp_name'])) {

$this->error='上傳方式不符合要求';

return flase;

}

return true;

}

//檢測文件類型

protected function checkMime(){

if (!is_array($this->allowMime,$this->endExt){

$this->error='文件類型MIME非法';

return flase;

}

return true;

}

//檢測是否為一個真實的圖片

protected function checekimage(){

if ($this->flage) {

if (!getimagesize($this->fileInfo['tmp_name'])) {

$this->error='文件不是一個真實的圖片';

return flase;

}

return ture;

}

}

//顯示錯誤

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;

//移動文件

if (@move_uploaded_file($this->fileInfo['tmp_name'],$this->destination)) {

return $this->destination;

}else{

$this->error='文件移動失??!';

$this->ShowError();

}


}else{

$this->ShowError();

}

}


}



??>


正在回答

3 回答

頂不住了?你這個BUG比我的還多?我還想復制粘貼省事然后整到一半重新自己寫了

0 回復 有任何疑惑可以回復我~

is_array改成in_array試試行不行

0 回復 有任何疑惑可以回復我~

https://img1.sycdn.imooc.com//5cd04c4d0001308507550482.jpg

第107行上下的代碼在這里...

0 回復 有任何疑惑可以回復我~

舉報

0/150
提交
取消
PHP實現文件上傳與下載
  • 參與學習       43750    人
  • 解答問題       338    個

本課程講解了文件上傳的原理和配置,學會兩種方式實現文件上傳與下載

進入課程

我又又又報錯了...

我要回答 關注問題
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號