<?php /*** 步驟:* 1.匯入inlcude本類檔案,如下函數upload 配置儲存路徑* 2.直接運用,配合前台,運行函數,注意前台form開啟enctype* 3.若運行成功,轉運檔案至指定目錄,獲得傳回值url物理全路徑* 4.把指定儲存的路徑存入資料庫*/ /** * 檔案上傳函數,把檔案儲存到指定路徑 * @param * $filename:表單裡寫的檔案名稱 name="XXX" ,會自動識別$_FILES * @return 成功返回圖片儲存物理全路徑,失敗返回false */ function upload($filename){ include_once(__ROOT__.'/common/Upload.class.php');//引入類檔案 $path = __ROOT__."/public/upload/".date("Y-m-d",time());//定義路徑 $upload = new upload($filename,$path);//執行個體化對象 $url = $upload->uploadFile();//調用成員方法,成功返回儲存的物理地址 if(empty($url)){ return false; } return $url; }/** * 修改會員資訊 * @param $id 哪位會員 $data修改的值[數組形式] * @return 成功返回影響行數 ;失敗返回false */ function update_user($id,$data){ $info = get_user($id);//查詢出此友情連結的資訊 //判斷有刪除圖片操作,清空圖片地址 if(@$data['del_pic']){ unlink(__ROOT__.'/public/'.$info['user_img']); unset($data['del_pic']); $data['user_img'] = " "; } if(!empty($_FILES['user_img']['name'])){ @unlink(__ROOT__."/public/".$info['user_img']);//先刪除原來檔案exit; $url = upload('user_img');//移動檔案 if($url === false){ return false; } $data['user_img'] = strstr($url,"upload");//賦新地址值 } $user = M('cms_user'); $data['password'] = md6($data['password'] ); $where = " user_id = $id "; $re = $user->where($where)->data($data)->update(); // echo $link->getLastSql();exit; if($re){ return true; } return false; }//--------------------------以上為類的應用方法----------------------//------------------------------類開始----------------------------- class Upload{ protected $fileName; //檔案名稱 protected $maxSize; //檔案最大大小 protected $allowMime; //允許的圖片範圍 protected $allowExt; //允許的檔案尾碼名 protected $uploadPath; //設定檔案上傳路徑 protected $imgFlag; //是否開啟檢測真實映像 protected $fileInfo; protected $error;//返回的檔案錯誤 protected $ext; //擷取的檔案尾碼 public function __construct($fileName='myFile',$uploadPath='./uploads',$imgFlag=true,$maxSize=5242880,$allowExt=array('jpeg','jpg','png','gif'),$allowMime=array('image/jpeg','image/png','image/gif')){ $this->fileName=$fileName; $this->maxSize=$maxSize; $this->allowMime=$allowMime; $this->allowExt=$allowExt; $this->uploadPath=$uploadPath; $this->imgFlag=$imgFlag; $this->fileInfo=$_FILES[$this->fileName]; } /** * 檢測上傳檔案是否出錯 * @return boolean */ protected function checkError(){ if(!is_null($this->fileInfo)){ if($this->fileInfo['error']>0){ switch($this->fileInfo['error']){ case 1: $this->error='超過了PHP設定檔中upload_max_filesize選項的值'; break; case 2: $this->error='超過了表單中MAX_FILE_SIZE設定的值'; 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{ return true; } }else{ $this->error='檔案上傳出錯'; return false; } } /** * 檢測上傳檔案的大小 * @return boolean */ protected function checkSize(){ if($this->fileInfo['size']>$this->maxSize){ $this->error='上傳檔案過大'; return false; } return true; } /** * 檢測副檔名s * @return boolean */ protected function checkExt(){ $this->ext=strtolower(pathinfo($this->fileInfo['name'],PATHINFO_EXTENSION)); if(!in_array($this->ext,$this->allowExt)){ $this->error='不允許的副檔名'; return false; } return true; } /** * 檢測檔案的類型 * @return boolean */ protected function checkMime(){ if(!in_array($this->fileInfo['type'],$this->allowMime)){ $this->error='不允許的檔案類型'; return false; } return true; } /** * 檢測是否是真實圖片 * @return boolean */ protected function checkTrueImg(){ if($this->imgFlag){ if(!@getimagesize($this->fileInfo['tmp_name'])){ $this->error='不是真實圖片'; return false; } return true; } } /** * 檢測是否通過HTTP POST方式上傳上來的 * @return boolean */ 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>'); } /** * 檢測目錄不存在則建立 */ protected function checkUploadPath(){ if(!file_exists($this->uploadPath)){ mkdir($this->uploadPath,0777,true); } } /** * 產生唯一字串 * @return string */ protected function getUniName(){ return md5(uniqid(microtime(true),true)); } /** * 上傳檔案 * @return string */ public function uploadFile(){ 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(); } } }