PHP多檔案上傳類
user_post_file = $file; $this->save_file_path = $path; $this->max_file_size = $size; //如果使用者不填寫檔案大小,則預設為2M. if ($type != '') $this->allow_type = $type; } /** * 儲存使用者上傳檔案,檢驗合法性通過後,儲存至指定位置。 * @access public * @return int 值為0時上傳失敗,非0表示上傳成功的個數。 */ function upload() { for ($i = 0; $i < count($this->user_post_file['name']); $i++) { //如果當前檔案上傳功能,則執行下一步。 if ($this->user_post_file['error'][$i] == 0) { //取當前檔案名稱、臨時檔案名稱、大小、副檔名,後面將用到。 $name = $this->user_post_file['name'][$i]; $tmpname = $this->user_post_file['tmp_name'][$i]; $size = $this->user_post_file['size'][$i]; $mime_type = $this->user_post_file['type'][$i]; $type = $this->getFileExt($this->user_post_file['name'][$i]); //檢測當前上傳檔案大小是否合法。 if (!$this->checkSize($size)) { $this->last_error = "The file size is too big. File name is: ".$name; $this->halt($this->last_error); continue; } //檢測當前上傳副檔名是否合法。 if (!$this->checkType($type)) { $this->last_error = "Unallowable file type: .".$type." File name is: ".$name; $this->halt($this->last_error); continue; } //檢測當前上傳檔案是否非法提交。 if(!is_uploaded_file($tmpname)) { $this->last_error = "Invalid post file method. File name is: ".$name; $this->halt($this->last_error); continue; } //移動檔案後,重新命名檔案用。 $basename = $this->getBaseName($name, ".".$type); //移動後的檔案名稱 $saveas = $basename."-".time().".".$type; //組合新檔案名稱再存到指定目錄下,格式:儲存路徑 + 檔案名稱 + 時間 + 副檔名 $this->final_file_path = $this->save_file_path."/".$saveas; if(!move_uploaded_file($tmpname, $this->final_file_path)) { $this->last_error = $this->user_post_file['error'][$i]; $this->halt($this->last_error); continue; } //儲存當前檔案的有關資訊,以便其它程式調用。 $this->save_info[] = array("name" => $name, "type" => $type, "mime_type" => $mime_type, "size" => $size, "saveas" => $saveas, "path" => $this->final_file_path); } } return count($this->save_info); //返回上傳成功的檔案數目 } /** * 返回一些有用的資訊,以便用於其它地方。 * @access public * @return Array 返回最終儲存的路徑 */ function getSaveInfo() { return $this->save_info; } /** * 檢測使用者提交檔案大小是否合法 * @param Integer $size 使用者上傳檔案的大小 * @access private * @return boolean 如果為true說明大小合法,反之不合法 */ function checkSize($size) { if ($size > $this->max_file_size) { return false; } else { return true; } } /** * 檢測使用者提交檔案類型是否合法 * @access private * @return boolean 如果為true說明類型合法,反之不合法 */ function checkType($extension) { foreach ($this->allow_type as $type) { if (strcasecmp($extension , $type) == 0) return true; } return false; } /** * 顯示出錯資訊 * @param $msg 要顯示的出錯資訊 * @access private */ function halt($msg) { printf(" %s
\n", $msg); } /** * 取副檔名 * @param String $filename 給定要取副檔名的檔案 * @access private * @return String 返回給定副檔名 */ function getFileExt($filename) { $stuff = pathinfo($filename); return $stuff['extension']; } /** * 取給定檔案檔案名稱,不包括副檔名。 * eg: getBaseName("j:/hexuzhong.jpg"); //返回 hexuzhong * * @param String $filename 給定要取檔案名稱的檔案 * @access private * @return String 返迴文件名 */ function getBaseName($filename, $type) { $basename = basename($filename, $type); return $basename; }}/******************** UploadFile類使用注釋//注意,上傳組件name屬性不管是一個還是多個都要使用數組形式,如://如果使用者點擊了上傳按鈕。if ($_POST['action'] == "上傳") { //設定允許使用者上傳的檔案類型。 $type = array('gif', 'jpg', 'png', 'zip', 'rar'); //執行個體化上傳類,第一個參數為使用者上傳的檔案組、第二個參數為儲存路徑、 //第三個參數為檔案最大大小。如果不填則預設為2M //第四個參數為充許使用者上傳的類型數組。如果不填則預設為gif, jpg, png, zip, rar, txt, doc,pdf $upload = new UploadFile($_FILES['user_upload_file'], 'j:/tmp', 100000, $type); //上傳使用者檔案,返回int值,為上傳成功的檔案個數。 $num = $upload->upload(); if ($num != 0) { echo "上傳成功
"; //取得檔案的有關資訊,檔案名稱、類型、大小、路徑。用print_r()列印出來。 print_r($upload->getSaveInfo()); //格式為: Array // ( // [0] => Array( // [name] => example.txt // [type] => txt // [size] => 526 // [path] => j:/tmp/example-1108898806.txt // ) // ) echo $num."個檔案上傳成功"; } else { echo "上傳失敗
"; }}*/?>?