又花了半天時間寫了個php上傳類解決方案

來源:互聯網
上載者:User
又花了半天時間寫了個php上傳類
剛學PHP沒多久,為了更好的練習及熟悉PHP,自己花了大半天寫了個php上傳類,在這裡做個筆記,歡迎朋友們對這個類做修改及最佳化。

PHP code
//up_class.php     * $upFolder(儲存到伺服器的檔案夾);
* $isRename(對上傳檔案重新命名,值 有 y|n );
* $reType(上傳成功後返回的值:n是返迴文件名,pn:返迴路徑和檔案名稱,j:返回js[待擴充]);
* $upExt(要上傳的檔案分類,因為在不同的表單要限制不同的上傳檔案類型,比如 A表單只能上傳圖片,B表單只能上偉壓縮包);
* $maxSize(限制上傳的檔案大小)
* @author:256kb * @2012-5-1 */ public function __construct($inputFile , $upExt = 0 , $reType = 'pn' , $upFolder = 'upload/' , $isRename = 'y' , $maxSize = 10485760){ $this->inputFile = $inputFile; $this->reType = $reType; $this->upMaxSize = $maxSize; $this->allowFile = $upExt; $this->upFolder = $upFolder; $this->isReName = $isRename; //$this->errorInt = -1; } public function upFile(){ $_file_arr = $_FILES[$this->inputFile]; $this->errorInt = $_file_arr['error']; if(is_uploaded_file($_file_arr['tmp_name'])){ if($_file_arr['tmp_name']){ $this->tmpName = $_file_arr['name']; $this->upMaxSize = $_file_arr['size']; $this->fileType = $_file_arr['type']; $this->tmpPath = $_file_arr['tmp_name']; $this->fileSize = $_file_arr['size']; if($this->upMaxSize > $this->upMaxSize){ $this->errorInt = 6 ; //大小超出網站限制 } if(!$this->isAllow()){ $this->errorInt = 8 ; //系統不允許此類型檔案 } if($this->isReName=='y'){ $this->savePath = $this->upFolder.$this->getFolder().'/'.$this->getNewName() ; $this->endFileName = $this->getNewName(); }else{ $this->savePath = $this->upFolder.$this->getFolder().'/'.$this->tmpName ; $this->endFileName = $this->tmpName; } //echo $this->errorInt; if(!$this->errorInt >= 1){ move_uploaded_file($this->tmpPath,$this->savePath); } } } } public function getFileUrl(){ switch($this->reType){ case 'n': return $this->endFileName; break; case 'pn': return $this->savePath; break; case 'js': return ""; break; default: return $this->savePath; } } //獲得新檔案名稱 public function getNewName(){ return substr($this->tmpName,1,strrpos($this->tmpName,".")-1).'_'.mktime().'.'.$this->getFileExt(); } public function upStatus(){ //echo $this->errorInt; switch ($this->errorInt){ case 1: return '超過了檔案大小php.ini中限制大小'; break; case 2: return '超過了檔案大小MAX_FILE_SIZE 選項指定的值'; break; case 3: return '檔案只有部分被上傳'; break; case 4: return '沒有檔案被上傳'; break; case 5: return '上傳檔案大小為0'; break; case 6: return '大小超出網站限制'; break; case 7: return '網站內沒有指定這種上傳類型'; break; case 8: return '系統不允許此類型檔案'; case 9: return '建立目錄失敗!'; break; } } private function isAllow(){ $allow = array( 0 => array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png','application/x-zip-compressed','application/octet-stream'), 1 => array('image/jpg','image/jpeg','image/png','image/pjpeg','image/gif','image/bmp','image/x-png'), 2 => array('application/x-zip-compressed','application/octet-stream'), 3 => array('','',''), 4 => array('','','') ); if($this->allowFile > count($allow)-1){ $this->errorInt = 7; //網站內沒有指定這種上傳類型 }else{ if(in_array($this->fileType, $allow[$this->allowFile])){ return true; }else{ return false; } } } public function getFileExt(){ //獲得副檔名 return strtolower(substr($this->tmpName,strrpos($this->tmpName,".")+1)); } private function getFolder(){ //獲得並自動建立相應檔案夾 if(strpos('|rar|zip|7z|iso|','|'.$this->getFileExt().'|')>=0){ $_folder = 'rar'; }elseif(strpos('|gif|jpeg|jpg|png|bmp|pjpeg|psd|','|'.$this->getFileExt().'|')>=0){ $_folder = 'img'; }elseif(strpos('|rm|rmvb|avi|mp4|swf|flv|wmv|','|'.$this->getFileExt().'|')>=0){ $_folder = 'vide'; }elseif(strpos('|doc|txt|xls|mdb||','|'.$this->getFileExt().'|')>=0){ $_folder = 'doc'; }else{ $_folder = 'other'; } if(!file_exists($this->upFolder.$_folder)){ if(!mkdir($this->upFolder.$_folder)){ $this->errorInt = 9; //建立目錄失敗 } } return $_folder; } }
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.