3. Handling Form-based File Upload with PHP

In this section, we will introduce to you how to handle form-based file uploads with a PHP script. The techniques are the same for HTML and XHTML. At the time of writing, the latest version of PHP is 5.2.0. So, we assume you are using PHP 5.2.0 in this tutorial. For other versions of PHP, the procedures should be similar. If you are not familiar with PHP, you may want to read some introductory tutorials before going through this section.


3.1. Parsing Form Data and Getting Information about the Uploaded File with PHP

To extract an uploaded file from an HTTP request, we need to parse the form data that is encoded in the "multipart/form-data" format. In PHP, the form data in an HTTP request is automatically parsed. The PHP engine stores the information of the uploaded files in the $_FILES array.

Now let's say the name attribute value of the <input type="file"> element in a certain HTML/XHTML/XHTML MP document is myFile. To obtain the information about the uploaded file, use the following lines of PHP script:


/* Get the size of the uploaded file in bytes. */
$fileSize = $_FILES['myFile']['size'];

/* Get the name (including path information) of the temporary file created by PHP that contains the same contents as the uploaded file. */
$tmpFile = $_FILES['myFile']['tmp_name'];

/* Get the name of the uploaded file at the client-side. Some browsers include the whole path here (e.g. e:\files\myFile.txt), so you may need to extract the file name from the path. This information is provided by the client browser, which means you should be cautious since it may be a wrong value provided by a malicious user. */
$fileName = $_FILES['myFile']['name'];

/* Get the content type (MIME type) of the uploaded file. This information is provided by the client browser, which means you should be cautious since it may be a wrong value provided by a malicious user. */
$contentType = $_FILES['myFile']['type'];


Nokia cell phones such as Nokia 6230 determine the content type (MIME type) of the file to be uploaded by its file extension. The following table lists some of the file extensions that are recognized by Nokia 6230:


File extension

Content type / MIME type

.jpg

image/jpeg

.gif

image/gif

.png

image/png

.wbmp

image/vnd.wap.wbmp

.txt

text/plain


If the Nokia 6230 cell phone does not recognize a file extension, it will specify "application/octet-stream" as the content type / MIME type of the file in the HTTP request.


Previous Page Page 3 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