3.3. Checking for Errors

One important thing that you should know is how to check whether there are any errors during the extraction of uploaded files from an HTTP request. If an error has occurred, PHP will set an error code, which can be retrieved using the following line of script (here we assume that the name attribute of the <input type="file"> element is myFile):


$errorCode = $_FILES['myFile']['error'];


If everything works fine, the value of $errorCode will be equal to the UPLOAD_ERR_OK constant. If there is an error, the value of $errorCode will be equal to one of the following constants:


3.3.1. UPLOAD_ERR_INI_SIZE Error Code

The UPLOAD_ERR_INI_SIZE error code means the size of the uploaded file is larger than the value specified by the upload_max_filesize directive.


3.3.2. UPLOAD_ERR_FORM_SIZE Error Code (Use with the MAX_FILE_SIZE Form Field)

The UPLOAD_ERR_FORM_SIZE error code never appears if there is no MAX_FILE_SIZE hidden field in the HTML/XHTML form that contains the <input type="file"> element.

The MAX_FILE_SIZE hidden field is used like this:


<form action="file_upload.php" method="post" enctype="multipart/form-data">
  <p>
    <input name="MAX_FILE_SIZE" value="1048576" type="hidden"/>
    <input name="myFile" type="file"/>
    <input type="submit"/>
  </p>
</form>


In the above HTML/XHTML code, the MAX_FILE_SIZE form field states that the file to be uploaded should not be larger than 1048576 bytes. If a browser supports this form field, it will not allow the user to choose a file that is larger than 1048576 bytes. So, the user does not have to wait for a file to upload to the server in order to find out whether it is too large or not. However, at the time of writing, we cannot find any WAP browsers that support the MAX_FILE_SIZE form field. Also, both IE 6 and Firefox 2.0 do not understand this form field.

No matter whether a web / WAP browser understands the MAX_FILE_SIZE form field or not, if the PHP engine finds that the uploaded file's size is larger than the value specified by the MAX_FILE_SIZE form field, it will give the error code UPLOAD_ERR_FORM_SIZE.

It is important to note that the MAX_FILE_SIZE field value can easily be modified at the client-side. So, the MAX_FILE_SIZE form field is not a reliable way to restrict the size of uploaded files. A better way is to use the upload_max_filesize directive, which is mentioned in "PHP Directives Related to (Large) File Upload" of this tutorial.

Note that the <input type="file"> element must be placed after the MAX_FILE_SIZE form field.


3.3.3. UPLOAD_ERR_PARTIAL Error Code

The UPLOAD_ERR_PARTIAL error code means the server only receives part of the uploaded file.


3.3.4. UPLOAD_ERR_NO_FILE Error Code

The UPLOAD_ERR_NO_FILE error code means the HTTP request contains no uploaded file.


3.3.5. UPLOAD_ERR_NO_TMP_DIR Error Code

The UPLOAD_ERR_NO_TMP_DIR error code means there is no temporary directory.


3.3.6. UPLOAD_ERR_CANT_WRITE Error Code

The UPLOAD_ERR_CANT_WRITE error code means the uploaded file cannot be written to disk.


Previous Page Page 5 of 11 Next Page


Feedback Form (ExpandCollapse)

What do you think about this web page?






(Optional) Please provide us more details. For example, suppose you select option 2 above, can you tell us specifically what information is missing? You can also suggest anything that can help us improve this web page.

(Optional) Your name:

(Optional) Your email address:

Please enter again to confirm:

Due to the amount of messages we received, we may not be able to reply to all messages.

A button for going back to the top of this page