MOCKSTACKS
EN
Questions And Answers

More Tutorials









PHP Handling file upload errors


The $_FILES["FILE_NAME"]['error'] (where "FILE_NAME" is the value of the name attribute of the file input, present in your form) might contain one of the following values:

1. UPLOAD_ERR_OK - There is no error, the file uploaded with success.
2. UPLOAD_ERR_INI_SIZE - The uploaded file exceeds the upload_max_filesize directive in php.ini.
3. UPLOAD_ERR_PARTIAL - The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML
form.
4. UPLOAD_ERR_NO_FILE - No file was uploaded.
5. UPLOAD_ERR_NO_TMP_DIR - Missing a temporary folder. (From PHP 5.0.3).
6. UPLOAD_ERR_CANT_WRITE - Failed to write file to disk. (From PHP 5.1.0).
7. UPLOAD_ERR_EXTENSION - A PHP extension stopped the file upload. (From PHP 5.2.0).

An basic way to check for the errors, is as follows:

<?php
$fileError = $_FILES["FILE_NAME"]["error"]; // where FILE_NAME is the name attribute of the file
input in your form
switch($fileError) {
 case UPLOAD_ERR_INI_SIZE:
 // Exceeds max size in php.ini
 break;
 case UPLOAD_ERR_PARTIAL:
 // Exceeds max size in html form
 break;
 case UPLOAD_ERR_NO_FILE:
 // No file was uploaded
 break;
 case UPLOAD_ERR_NO_TMP_DIR:
 // No /tmp dir to write to
 break;
 case UPLOAD_ERR_CANT_WRITE:
 // Error writing to disk
 break;
 default:
 // No error was faced! Phew!
 break;
}
?>


Conclusion

In this page (written and validated by ) you learned about PHP Handling file upload errors . What's Next? If you are interested in completing PHP tutorial, your next topic will be learning about: PHP Passing arrays by POST.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.