Uploading Files using PHP

PHP allow user to select files to upload into a specified directory. For that, user need to send the details for the file through HTML forms. These details are processed in the PHP Script by using the super global variable named $_FILES. File upload can be implemented by following the below steps.

  1. Create HTML form with required element to choose file.
  2. Specify the directory path as target to the uploaded file.
  3. Check whether $_FILES global array not empty.
  4. Ensure that the selected file is available in the temporary directory
  5. And then, call function to upload file from temporary location to the user specified target.

Each of the above steps include just a single line of code or two. But, these steps describe the flow of control over the file upload process clearly. Let us see about each step with suitable code samples.

php_file_upload

Create HTML form

Usually form tag contains three attributes like name, action and method as we have seen while discussing about HTML Forms. Added to that, we are required to specify the enctype attribute with the form tag. This should have the value as multipart/form-data.

Form method must be POST and also it must have the input field with type as file. So, HTML form will look like,

<form action="" enctype="multipart/form-data" method="POST" name="frm_user_file">
<input type="file" name="myfile" /> 
<input type="submit" name="submit" value="Upload" />
</form>

Specify target directory

This step is implemented in PHP script, unlike last previous one. For that, the path of the target directory will be initialized to a variable as follows.

$targetDir = "D:\phppot_uploads";

We should create such directory before specifying it. This path will be referred while uploading selected file.

Check whether $_FILES is empty or not

Since, PHP global array $_FILES has the details about the file to be uploaded, we need to check whether the global array is not empty. If so, we can further proceed with the file information.

if(is_array($_FILES)) {
...
}

We can use either is_array() function of php to check if $_FILES has the required file information or not.

Ensuring about the availability of file in temporary location

For that, is_uploaded_file() function is used, for which we need to put the temporary directory location as its argument. This will be stored into the $_FILES named as tmp_name. The following code will show this clearly.

if(is_uploaded_file($_FILES['myfile']['tmp_name'])) {
...
}

Uploading file after all availability checking

In PHP, move_uploaded_file() method is used to upload a selected file, by having both the source and destination path of the file. So, file selected by the user which is stored in temporary location will be uploaded into the specified target location with this step. The following code is showing the usage of move_uploaded_file() function with entire availability checking process discussed above.

$targetDir = "D:\phppot_uploads";
if(is_array($_FILES)) {
if(is_uploaded_file($_FILES['myfile']['tmp_name'])) {
if(move_uploaded_file($_FILES['myfile']['tmp_name'],"$targetDir/".$_FILES['myfile']['name'])) {
echo "File uploaded successfully";
}
}
}

Download PHP File Upload Source Code

This PHP code tutorial was published on May 2, 2013.

↑ Back to Top