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 was uploaded.
  2. UPLOAD_ERR_INI_SIZE - The uploaded file exceeds the upload_max_filesize directive in php.ini.
  3. UPLOAD_ERR_FORM_SIZE - The uploaded file exceeds the MAX_FILE_SIZE value that was specified in the HTML form.
  4. UPLOAD_ERR_PARTIAL - The file was only partially uploaded.
  5. UPLOAD_ERR_NO_FILE - No file was uploaded.
  6. UPLOAD_ERR_NO_TMP_DIR - Missing a temporary folder. (From PHP 5.0.3.)
  7. UPLOAD_ERR_CANT_WRITE - Failed to write the file to disk. (From PHP 5.1.0.)
  8. UPLOAD_ERR_EXTENSION - A PHP extension stopped the file upload. (From PHP 5.2.0.)

A basic way to check the errors is:

<?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_OK:
        // The file was uploaded
        break;
    case UPLOAD_ERR_INI_SIZE:
        // Exceeds upload_max_filesize in php.ini
        break;
    case UPLOAD_ERR_FORM_SIZE:
        // Exceeds MAX_FILE_SIZE in the HTML form
        break;
    case UPLOAD_ERR_PARTIAL:
        // The file was only partially uploaded
        break;
    case UPLOAD_ERR_NO_FILE:
        // No file was uploaded
        break;
    case UPLOAD_ERR_NO_TMP_DIR:
        // No temporary directory to write to
        break;
    case UPLOAD_ERR_CANT_WRITE:
        // Error writing to disk
        break;
    case UPLOAD_ERR_EXTENSION:
        // An extension stopped the upload
        break;
}

Feedback about page:

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



Table Of Contents