Handling file upload errors

suggest change

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;
}

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:


Reading request data:
* Handling file upload errors

Table Of Contents
2 Arrays
4 Types
10 Cookies
14 JSON
15 SOAP
17 cURL
19 XML
21 Traits
35 UTF-8
36 URLs
38 PHPDoc
41 Loops
44 Closur
45 Reading request data
72 YAML
77 Cache
78 Streams
81 PDO
82 SQLite3
83 Sockets
87 MongoDB
93 IMAP
94 Redis
95 Imagick
102 APCu
108 PSR