Checking for Errors

Preferences - Do not show ads

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:


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.


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.


UPLOAD_ERR_PARTIAL Error Code

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


UPLOAD_ERR_NO_FILE Error Code

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


UPLOAD_ERR_NO_TMP_DIR Error Code

The UPLOAD_ERR_NO_TMP_DIR error code means there is no temporary directory.


UPLOAD_ERR_CANT_WRITE Error Code

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

Preferences - Do not show ads


Page 5 of 11 Previous Page | Next Page
Contents at a Glance
Print this Web Page | | Back to Top

Feedback Form (Expand)

What do you think about this web page?






Upload File from WAP Cell Phone - Table of Contents Upload File from WAP Cell Phone - Contents at a Glance Preferences - Change Color Scheme Preferences - Do Not Show Ads Previous Page Next Page