File upload class
1. Determine the member attributes of the file upload class.
First of all, we must consider what attributes are needed to upload the file. We can write a post method for the file file at will. Submit the form, then print to see what parameters are in $_FILES
Create a new file_upload_html.html file:
The code is as follows:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h2>用戶頭像上傳</h2> <p>用戶姓名:張三</p> 現(xiàn)有頭像:<img src="" onerror="this.src='./default.jpg'"> <form method="post" enctype="multipart/form-data"> <input type="file" name="photo"><br> <input type="submit" value="上傳"> </form> </body> </html>
Get the parameters submitted by the form, Create a new file_upload_object.php, the code is as follows
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/3 0003 * Time: 上午 11:33 */ require './file_upload_html.html'; //獲取表單提交的參數(shù) $file=isset($_FILES['photo'])?$_FILES['photo']:""; echo "<pre>"; print_r($file); echo "</pre>";
After submitting the form, the print result is as follows:
Therefore It can be known that the upload file class needs to knowthe uploaded file name, file type, error flag, and uploaded size
So when we write an upload file class, we need to know these attributes, create a new An upload file class, FileUpload.Class.php
code is as follows:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/3 0003 * Time: 上午 10:52 */ class FileUpload{ public $type=array( 'image/jpeg','image/pjpeg','image/png','image/x-png','image/gif' ); public $maxSize=1048576;//1M=1024*1024 public $uploadSavePath="./uploads"; public $errorMessage=""; }
2, method of file upload class
Write a method to manage the attributes of the file upload class according to your own business needs.
For example, the setting of the upload size and the setting of the upload saving path, the method name is upload( )
The specific code is as follows:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/3 0003 * Time: 上午 10:52 */ class FileUpload{ private $type=array( 'image/jpeg','image/pjpeg','image/png','image/x-png','image/gif' );//文件類(lèi)型 public $maxSize=1048576;//1M=1024*1024,上傳的容量 public $uploadSavePath="./uploads"; //上傳保存的路徑 public $errorMessage=""; //錯(cuò)誤信息 public function upload($file){ //上傳類(lèi)首先得判斷上傳存不存在錯(cuò)誤,錯(cuò)誤信息有123467六種情況,0為正常上傳 if($file['error']>0){ $errorMsg="錯(cuò)誤信息為:"; switch ($file['error']){ case 1:$errorMsg.="文件大小超過(guò)了php.ini中upload_max_filesize選項(xiàng)限制的值"; break; case 2:$errorMsg.="文件大小超過(guò)了表單中max_file_size選項(xiàng)指定的值!"; break; case 3:$errorMsg.="文件只有部分被上傳!"; break; case 4:$errorMsg.="沒(méi)有文件被上傳!"; break; case 6:$errorMsg.="找不到臨時(shí)文件夾!"; break; case 7:$errorMsg.="臨時(shí)文件寫(xiě)入失敗"; break; default:$errorMsg.='未知錯(cuò)誤!'; break; } return false; } //判斷上傳的文件是否屬于$type內(nèi) if(!in_array($file['type'],$this->type)){ //不在所屬類(lèi)型內(nèi)時(shí) $this->errorMessage="未定義的文件上傳類(lèi)型"; return false; } //判斷文件上傳的大小不能超過(guò)所定義的大小 if($file['size']>$this->maxSize){ $this->errorMessage="超出上傳所限制的最大上傳容量"; return false; } //給上傳的圖片重命名 $newFileName=uniqid("php_").strrchr($file['name'],"."); //設(shè)置上傳文件的全目錄 ./uploads/2018-03-03 $allPath1=$this->uploadSavePath."/".date("Y-m-d"); $allPath=$this->uploadSavePath."/".date("Y-m-d")."/".$newFileName; //判斷這個(gè)目錄是否存在 if(!file_exists($allPath1)){ mkdir($allPath1,'0777',true); } //移動(dòng) if(!move_uploaded_file($file['tmp_name'],$allPath)){ $this->errorMessage="文件上傳失敗"; }else{ return $allPath; }; } }
3, file upload processing
Perform class Call the object of the generated file upload class and print the error message
Modify the code in file_upload_object.php as follows:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2018/3/3 0003 * Time: 上午 11:33 */ require './FileUpload.class.php'; $file=isset($_FILES['photo'])?$_FILES['photo']:""; $fileUpload=new FileUpload(); $allPath=$fileUpload->upload($file); if(!$allPath){ //上傳失敗,打印錯(cuò)誤信息 echo $fileUpload->errorMessage; //結(jié)束運(yùn)行 die(); } require './file_upload_html.html';
Modify the src attribute on the html page:
The code is as follows:
<?php 現(xiàn)有頭像:<img src="<?php echo $allFilePath; ?>" onerror="this.src='./default.jpg'">
Click to select the file, click upload after selecting, the result As follows:
Thinking: Obviously the uploaded avatar is too big, how to reduce the uploaded avatar A little?
(See [PHP] File and Image Tutorial for detailed steps)
##