PHP File Upload

Summary: in this tutorial, you will learn how to upload files to a web server in PHP. We will give you an example of uploading a picture to the webserver and display it on a webpage.

Creating PHP file upload form

First, to upload a file to the server, you need to have a file upload form. In addition to action and method attributes, the file upload form must have the enctype attribute with the value as multipart/form-data:

The enctype attribute allows the data of the form to be encoded as multipart MIME data required for uploading binary data to the webserver.

Second, inside the upload file form, you need to have a file-select control so that users can select the file from their computers to upload to the server.

You can have as many file-select fields as you want in a form. The following example shows you how to create a file select field:

Notice that we used type="file" for the <input> element. So the PHP upload file form to the server is as follows:

How PHP handles uploaded file

You can access the information on the uploaded file using $_FILES superglobal array. The following example shows you how to access uploaded a file with the file field named pictureFile:

This array element itself is also an associative array that contains various information about the uploaded file as described follows:

  • name: the name of the uploaded file
  • type: the MIME type of the upload file e.g., image/jpeg for JPEG image or application/pdf for PDF file.
  • size: the size of the uploaded file in bytes
  • tmp_name: the full path to the uploaded file stored in a temporary folder in the webserver.
  • error: the error code that describes the status of file upload e.g., UPLOAD_ERR_OK means the file was uploaded successfully.

For example, to access file name of the uploaded file, you use the following code snippet:

PHP allows you to limit the size of the uploaded file via upload_max_filesize directive in the php.ini file. The default upload_max_filesize is typically 2 megabytes. To upload a bigger file, you need to adjust this directive.

When the file is uploaded successfully to the web server, it is stored in a temporary folder. Typically, you use the move_uploaded_file() function to move the file from the temporary folder to your specific folder. It accepts two arguments:

  • filename: is the full path to the file in the temporary folder. You can access it using the tmp_name key.
  • destination: the folder that the uploaded file is moved to.

The move_uploaded_file() function returns true if it moved the file successfully, or false if there was an error.

Putting it all together

The following example shows you how to upload a JPEG photo to a server and displays it.

First, create a function to display the file upload form:

Second, we create a function to handle file upload.

  • If the form data is not submitted, we display the file upload form above so that users can choose a file to upload.
  • If the form data is submitted, we check to see if there is an error that occurred for file upload and makes sure that the uploaded file is JPEG. If everything is correct, we move the uploaded file to the images/ folder and display the uploaded file using the display_picture() function, otherwise, we issue an error message.

The script is as follows:

To run the script you have to create a folder named images/ in your web app root path and make it writable. You can download the PHP file upload project in the following link and play with it in your system.

PHP File Upload Script (1986 downloads)

 

  • Was this tutorial helpful ?
  • YesNo