使用php時常會遇到要讓使用者上傳檔案的情況
除了在form要設定enctype="multipart/form-data"之外
還可以使用官方所提供的error_code來得知檔案上傳的情況
以下說明的error code自php 4.3.0版開始成為內定常建功能
A.代號是 0 ;上傳正常,且完成上傳
UPLOAD_ERR_OK
Value: 0; There is no error, the file uploaded with success.
B.代號是 1 ;上傳失敗,檔案size超過php.ini裡所設定的 [可上傳檔案的大小]
UPLOAD_ERR_INI_SIZE
Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.
C.代號是 2 ;上傳失敗,檔案size超過php.ini裡所設定的 [可接受的檔案大小]
UPLOAD_ERR_FORM_SIZE
Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.
D.代號是 3 ;上傳失敗,僅有部份檔案上傳完成
UPLOAD_ERR_PARTIAL
Value: 3; The uploaded file was only partially uploaded.
E.代號是 4 ;上傳失敗,因為沒有檔案上傳
UPLOAD_ERR_NO_FILE
Value: 4; No file was uploaded.
F.代號是 6 ;上傳檔案所須的tmp資料找不到
UPLOAD_ERR_NO_TMP_DIR
Value: 6; Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.
G.代號是 7 ;將檔案寫入磁碟時發生錯誤
UPLOAD_ERR_CANT_WRITE
Value: 7; Failed to write file to disk. Introduced in PHP 5.1.0.
H.代號是 8 ;你的版本的php的某項延伸功能阻檔了檔案上傳
UPLOAD_ERR_EXTENSION
Value: 8; A PHP extension stopped the file upload. PHP does not provide a way to ascertain which extension caused the file upload to stop; examining the list of loaded extensions with phpinfo() may help. Introduced in PHP 5.2.0.
=========分===========隔============線======================
所以,根據上述的error code
我們可以初步的進行檔案上傳的狀態篩選
例如
if ($_FILES[your form file input name]['error'] == 4) -> 可用來偵測使用者有沒有上傳檔案
ps. [your form file input name] 是指 myfile
<form method="post" enctype="multipart/form-data">
<input type="file" name="myfile">
</form>
以上資料參考來源 http://www.php.net/manual/en/features.file-upload.errors.php
留言列表