How to upload a file in php

How to upload a file in php

In this tutorial we are going to discuss how we can upload a file or image in PHP. File uploads are an important feature for many web applications.  We use file uploads in many website we use like Facebook, or Gmail, Yahoo, Hotmail or in job sits and  lot  of other sites.

So let us explore what goes behind the scenes for uploading a file using PHP.

There are two things we have to look in PHP file uploading.

1. Client side

2. Server Side

First let us examine what we need to do  on client or front end while uploading a file. Consider following code snippet.

file upload in php

php file uplaod

Consider the line below.  There are two main things to do in form tag.

php file upload form header

One is that have started a form tag. In form tag and have included a very important attribute. That is enctype and is value is set to

This attribute is called encoding type and it  must  be added in form beginning tag while uploading a file otherwise file or image will not be uploaded.

Beginners in PHP programming do this mistake that they forget to include enctype in form tag and their script does not work. enctype attribute tells form that how form data will be encoded  when we will submit it to server.

Note: For file upload we have to use “multipart/form-data” Other values for enctype are application/x-www-form-urlencoded and “text/plain”.

Second most important thing is to set method=”post”. Without method=post file will also not upload. So after setting these  two attributes in form tag we move to the next lines of code

php file uplaod form fields

These line are straight forward, there is an HTML file element used and a submit button.

Now when user click submit button form data will be posted to php page in “action” attribute.  (If action attribute is not specified then form will be posted to current page).

There are certain parameters that we have to take care of.

  • By default php uploads an image size upto 2MB.  (Image or file with size greater than 2 MB will not be uploaded)
  • A Post request (included all form fields data) will only send upto 8MB  of data.

Note: These values can be changed in php.ini file. We have to change two attributes

In php.ini file just change these parameters, save file and restart Apache server.

Now on server side once a file is uploaded to the server, PHP stores file or image in a temporary location and makes it available to the script that was called by the POST transaction  and when our php script moves the file to a safe location, the temporary copy of file is automatically destroyed when the script execution is finished.

Now to access file in PHP code it is to remember that uploaded files always appear in the $_FILES superglobal array. Normal form element data is accessed through $_POST array but uploaded file will be in $_FILES array. 

$_FILES contains following information about an uploaded file.

  • file name                                                         The original name of the file
  • file type                                                           The MIME type of the file provided by the browser
  • file size                                                            The size (in bytes) of the file
  • tmp_name                                                      The name of the file’s temporary location
  • Error                                                                The error code associated with this file.

A value of  UPLOAD_ERR_OK indicates a successful transfer, while any other  error indicates that something went wrong             (for example, the file was bigger than the maximum allowed size).

Consider following code snippet.

$_FILES[“file”][“name”] – the name of the uploaded file
$_FILES[“file”][“type”] – the type of the uploaded file
$_FILES[“file”][“size”] – the size in bytes of the uploaded file
$_FILES[“file”][“tmp_name”] – the name of the temporary copy of the file stored on the server
$_FILES[“file”][“error”] – the error code resulting from the file upload

Applying Validation

To apply validation on uploaded file type we can use following code.

php is file uploaded function

tells the mime type of  file. For images it can be

is_uploaded_file function.

In php file upload is_uploaded_file is very important function.  this function confirms that  the file was uploaded via HTTP POST and user has browsed and uploaded a file. See following code snippet.

is_uploaded_file function

In this code snippet consider the line

This line checks whether user has actually selected a file to upload so this function is must to use for validation and to move file to our desired location. Next important function is

Consider following code snippet.

This function moves

php file upload move_uploaded_file

Inside

we have use a function

this function accepts two parameters one is existing file that is temporary file inside temporary directory in server. Second parameter is the destination path with file name. That in this case is upload  directory and

is original file name that user selected to upload. We can change the name of file if we desire. So

will move file to upload directory of server. We have to first create this directory so our file can be moved to specified folder.

Complete code for file upload is as under.

First we have checked that whether form is posted and user clicked submit button. then we make use of

Validate if user has selected and uploaded a file from system. next after validating file types, file is moved to upload directory from temporary directory.

upload a file in php - complete code

Comments

  1. By Sourish

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.