php resource centre

  • about
  • articles
  • tutorials
  • resources
  • certification
Home

Primary links

  • About
  • Articles
  • Tutorials
  • Resources
  • Certification

PHP File Upload Errors Explained

admin — Wed, 27/06/2007 - 10:12am

From version 4.2.0, PHP has got a new set of error codes that handles and reports all errors occuring during file upload, in the filearray object. The error is found in $_FILES['userfile']['error'].These error codes  and their explanations are given in this article.


UPLOAD_ERR_OK (error 0)
Means : File upload was successful.

UPLOAD_ERR_INI_SIZE (error 1)
Means : The uploaded file exceeds the upload_max_filesize directive in php.ini.

UPLOAD_ERR_FORM_SIZE (error 2)
Means : The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.

UPLOAD_ERR_PARTIAL (error 3)
Means : The uploaded file was only partially uploaded.

UPLOAD_ERR_NO_FILE (error 4)
Means : No file was uploaded.

UPLOAD_ERR_NO_TMP_DIR (error 6)
Means : Missing a temporary folder. Introduced in PHP 4.3.10 and PHP 5.0.3.

UPLOAD_ERR_CANT_WRITE (error 7)
Means : Failed to write file to disk. Introduced in PHP 5.1.0.

UPLOAD_ERR_EXTENSION (error 8)
Means : File upload stopped by extension. Introduced in PHP 5.2.0.

Note: These became PHP constants in PHP 4.3.0.

 

The following code snippet can be used toeffectively debug your upload code.

<?
if ($_REQUEST['Submit'] == "Send File") {
    $uploaddir = '/home/yourserver/myfiles/';
    $uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
    echo '<pre>';
    if (move_uploaded_file($_FILES['userfile'][
    'tmp_name'], $uploadfile)) {
        echo "File is valid, and was successfully uploaded.\n";
    } else {
        switch ($_FILES['userfile']["error"]) {
            case 1:
                echo("The uploaded file exceeds the upload_max_filesize directive (".ini_get("upload_max_filesize").") in php.ini.");
                break;
            case 2:
                echo("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.");
                break;
            case 3:
                echo("The uploaded file was only partially uploaded.");
                break;
            case 4:
                echo("No file was uploaded.");
                break;
            case 6:
                echo("Missing a temporary folder.");
                break;
            default:
                echo("An unknown file upload error occured");
                break;
        }
    }
}
?>
<form enctype="multipart/form-data" action="" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="1024000" />
Send this file: <input name="userfile" type="file" />
<input type="submit" name="Submit" value="Send File" />
</form>

  • Tutorials
  • Login to post comments

User login

  • Request new password

Follow Us

Who's online

There are currently 0 users and 1 guest online.

Who's new

  • Nisha
  • linnaeus
  • Yameen
  • TalleyReedy
  • admin

Follow vipin7873 on Twitter

<!-- Start of Woopra Code -->

<!-- End of Woopra Code -->

  • about
  • articles
  • tutorials
  • resources
  • certification

copyright © 2010 Vipin Chandran