老師你好,我根據(jù)你講解的代碼編寫后,封裝上傳文件對象后,上傳文件總是報文件擴展類型錯誤,調(diào)試后fileInfo信息位NULL
<?php
/**
* Created by PhpStorm.
* User: andin
* Date: 16-3-18
* Time: 上午9:29
*/
class upload{
? ?protected $fileName;
? ?protected $maxSize;
? ?protected $allowMime;
? ?protected $allowExt;
? ?protected $uploadPath;
? ?protected $imgFlag;
? ?protected $fileInfo;
? ?protected $error;
? ?protected $ext;
? ?/**
? ? * @param string $fileName
? ? * @param string $uploadPath
? ? * @param int $maxSize
? ? * @param bool $imgFlag
? ? * @param array $allowMime
? ? * @param array $allowExt
? ? */
? ?public function _construct($fileName='myfile',$uploadPath='./upload',$maxSize=5242880,$imgFlag=true,$allowMime=array('image/jpeg','image/png','image/gif','image/jpg'),$allowExt=array('jpg','jpeg','txt','png','gif','bmp')){
? ? ? ?$this->fileName=$fileName;
? ? ? ?$this->maxSize=$maxSize;
? ? ? ?$this->allowMime=$allowMime;
? ? ? ?$this->allowExt=$allowExt;
? ? ? ?$this->uploadPath=$uploadPath;
? ? ? ?$this->imgFlag=$imgFlag;
? ? ? ?$this->fileInfo=$_FILES[$this->fileName];
? ?}
? ?/**
? ? * 檢測是否出錯
? ? * @return bool
? ? */
? ?protected function checkError(){
? ? ? ?//if(!is_null($this->fileInfo)) {
? ? ? ? ? ?if ($this->fileInfo['error'] > 0) {
? ? ? ? ? ? ? ?switch ($this->fileInfo['error']) {
? ? ? ? ? ? ? ? ? ?case 1:
? ? ? ? ? ? ? ? ? ? ? ?$this->error = '超過了php配置文件中upload選項的值';
? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ?case 2:
? ? ? ? ? ? ? ? ? ? ? ?$this->error = '超過了表單中的限制';
? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ?case 3:
? ? ? ? ? ? ? ? ? ? ? ?$this->error = '文件上傳不完整';
? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ?case 4:
? ? ? ? ? ? ? ? ? ? ? ?$this->error = '沒有選擇上傳目錄';
? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ?case 6:
? ? ? ? ? ? ? ? ? ? ? ?$this->error = '沒有找到臨時目錄';
? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ?case 7:
? ? ? ? ? ? ? ? ? ? ? ?$this->error = '文件不可寫';
? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ? ? ?case 8:
? ? ? ? ? ? ? ? ? ? ? ?$this->error = '由于php擴展程序中斷上傳';
? ? ? ? ? ? ? ? ? ? ? ?break;
? ? ? ? ? ? ? ?}
? ? ? ? ? ? ? ?return false;
? ? ? ? ? ?}else {
? ? ? ? ? ? ? ?//echo 'aa';
? ? ? ? ? ? ? ?return true;
? ? ? ? ? ?}
? ? ? ?//} else{
? ? ? ?// ? ? ? ?$this->error='文件上傳錯誤';
? ? ? ?// ? ? ? ?return false;
? ? ? ? // ? }
? ?}
? ?/**
? ? * 檢測上傳文件大小
? ? * @return bool
? ? */
? ?protected function checkSize(){
? ? ? ?if($this->fileInfo['size']>$this->maxSize){
? ? ? ? ? ?$this->error='上傳文件過大';
? ? ? ? ? ?return false;
? ? ? ?}
? ? ? ?return true;
? ?}
? ?/**
? ? *檢測擴展名
? ? * @return bool
? ? */
? ?protected function checkExt(){
? ? ? // $this->ext=strtolower(pathinfo($this->fileInfo['name'],PATHINFO_EXTENSION));
? ? ? ?$this->ext=strtolower(pathinfo($this->fileInfo['name'],PATHINFO_EXTENSION));
? ? ? ?echo 'bb';
? ? ? ?//var_dump($this->fileInfo['name']);
? ? ? ?var_dump($this->ext);
? ? ? ?if(!in_array($this->ext,$this->allowExt)){
? ? ? ? ? ?$this->error='不允許的擴展名';
? ? ? ? ? ?return false;
? ? ? ?}
? ? ? ?return true;
? ?}
? ?/**
? ? * 檢測文件類型
? ? * @return bool
? ? */
? ?protected function checkMime(){
? ? ? ?if(!in_array($this->fileInfo['type'],$this->allowMime)){
? ? ? ? ? ?$this->error='不允許的文件類型';
? ? ? ? ? ?return false;
? ? ? ?}
? ? ? ?return true;
? ?}
? ?/**
? ? * 檢測圖片是否真實
? ? * @return bool
? ? */
? ?protected function checkTrueImg(){
? ? ? ?if($this->imgFlag){
? ? ? ? ? ?if(!@getimagesize($this->fileInfo['tmp_name'])){
? ? ? ? ? ? ? ?$this->error='不是真實的圖片';
? ? ? ? ? ? ? ?return false;
? ? ? ? ? ?}
? ? ? ? ? ?return true;
? ? ? ?}
? ?}
? ?/**
? ? * 檢測文件是否通過http post方式上傳
? ? * @return bool
? ? */
? ?protected function checkHTTPPost(){
? ? ? ?if(!is_uploaded_file($this->fileInfo['tmp_name'])){
? ? ? ? ? ?$this->error='文件不是通過http post方式上傳的';
? ? ? ? ? ?return false;
? ? ? ?}
? ? ? ?return true;
? ?}
? ?/**
? ? * 獲取錯誤信息
? ? */
? ?protected function showError(){
? ? ? ?exit('<span style="color: red">'.$this->error.'</span>');
? ?}
? ?/**
? ? * 檢測目錄不存在就創(chuàng)建
? ? */
? ?protected function checkUploadPath(){
? ? ? ?if(!file_exists($this->uploadPath)){
? ? ? ? ? ?mkdir($this->uploadPath,0777,true);
? ? ? ?}
? ?}
? ?/**
? ? * 產(chǎn)生唯一字符竄當文件名
? ? * @return string
? ? */
? ?protected function getUniName(){
? ? ? ?return md5(uniqid(microtime(true),true));
? ?}
? ?/**
? ? * 上傳文件
? ? * @return string
? ? */
? ?public function loadFile(){
? ? ? ?var_dump($this->fileInfo['name']);
? ? ? ?if($this->checkError()&&$this->checkSize()&&$this->checkExt()&&$this->checkMime()&&$this->checkTrueImg()&&$this->checkHTTPPost()){
? ? ? ? ? ?$this->checkUploadPath();
? ? ? ? ? ?$this->uniName=$this->getUniName();
? ? ? ? ? ?$this->destination=$this->uploadPath.'/'.$this->uniName.'.'.$this->ext;
? ? ? ? ? ?if(@move_uploaded_file($this->fileInfo['tmp_name'],$this->destination)){
? ? ? ? ? ? ? ?return $this->destination;
? ? ? ? ? ?}else{
? ? ? ? ? ? ? ?$this->error='文件移動失敗';
? ? ? ? ? ? ? ?$this->showError();
? ? ? ? ? ?}
? ? ? ?}else{
? ? ? ? ? ?$this->showError();
? ? ? ?}
? ?}
}
2016-03-27
你視頻沒有看完吧?