# 筆記_PHP_UploadFile_很重要多看!!! --- ###### tags: `檔案上傳` `PHP` --- 我個人對於PHP的檔案上傳有很大的疑問 這次來多練練 分成單檔案上傳跟多檔案上傳 - $_File error - 1. 確認有沒有檔案傳過來 2. 確認type有沒有符合上傳條件 4. 確認有沒有uploadfile資料夾在目錄下 5. 如果沒有就產生一個 有就略過 6. 檢查資料夾中是否有重複檔案 7. 沒有重複的話 從暫存資料夾搬到upload下 8. 回覆使用者 --- ## $_File error 這篇寫得很好!!! 常常寫上傳檔案卻總是不知道error在幹嘛... 請多看!!! 引用 [php 檔案上傳error code [handling file upload error code]](https://jscorpio.pixnet.net/blog/post/32001366-php-%E6%AA%94%E6%A1%88%E4%B8%8A%E5%82%B3error-code-%5Bhandling-file-upload-error-code%5D) 使用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 我們可以初步的進行檔案上傳的狀態篩選 例如 ```php= if ($_FILES[your form file input name]['error'] == 4) // -> 可用來偵測使用者有沒有上傳檔案 ``` ps. \[your form file input name] 是指 myfile ```htmlembedded= <form method="post" enctype="multipart/form-data"> <input type="file" name="myfile"> </form> ``` --- 代號是 7 ;將檔案寫入磁碟時發生錯誤 有可能是 資料夾沒有權限 或是 臨時資料夾(暫存文件夾)沒有權限 或是不存在 --- ### 定義的錯誤常数
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up