Skip to content
PHP file upload by using $_FILES array
In This Tutorial
Uploading files by using PHP
This chapter will take you through how to upload files by using PHP script. File upload is required in many scenarios to allow users of your site to select files from their local computers and upload to your website. These files may include image uploads, pdfs, audio or video files – depending on the requirements.
What is PHP $_FILES?
PHP provides $_FILES, which is a built-in array. The PHP $_FILES array contains all information regarding uploaded files.
Handling file attributes are managed in this array, for example, a file name, size, file type (.jpg, .gif, pdf, png etc.). This chapter will show you how to use the $_FILES array; see the following script.
An example of uploading a file by PHP
Let us go step by step to learn how to upload a file in PHP by using a real example:
Step 1 – A basic user interface to select a file
First, we will go through uploading a single file by using the PHP script. The basic code below contains HTML code of a form.
It allows the users to select a file from the local computer and then press “Upload” button to upload a file on the hosting server (if you are practicing at the local computer, this is your server as well). Save this code as testupload.html
Experience this example online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<html>
<head>
<title>A simple example of uploading a file using PHP script</title>
</head>
<body>
<b>Simple example of uploading a file</b><br />
Choose a file to be uploaded<br />
<form action=“upload_file.php” method=“post” enctype=“multipart/form-data”>
<input type=“file” name=“file” size=“50” />
<br />
<input type=“submit” value=“Upload” />
</form>
</body>
</html>
|
Step 2 – Uploading file to the server script
Now follow these points:
- Create a PHP file and name it file_uploader.php. This is the file name that we used in our form action=”upload_file.php” in step 1. This file contains uploading script.
- Create a folder and name it testupload. The location of the folder must be the same where upload_file.php exists (for this example).
- Now place the following code in upload_file.php file.
Experience this example online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?php
$targetfolder = “testupload/”;
$targetfolder = $targetfolder . basename( $_FILES[‘file’][‘name’]) ;
if(move_uploaded_file($_FILES[‘file’][‘tmp_name’], $targetfolder))
{
echo “The file “. basename( $_FILES[‘file’][‘name’]). ” is uploaded”;
}
else {
echo “Problem uploading file”;
}
?>
|
That’s it!
Now let us go through the upload_file.php code to learn what PHP script is doing!
- The $targetfolder variable contains the location where files should be uploaded. Change it as per the requirement of the project. In our example, you can see our target folder testupload is relative to our script file. If your script file is in the following location, for example, www.yourdomain.com/site/upload_file.php then uploaded files will be at www.yourdomain.com/site/testupload.
- The file upload script then uses the move_uploaded_file() function to move uploaded file to the specified location, testupload, in that case. This function uses the $_FILE array.
- If the file is uploaded then it will show upload success message with the file name otherwise an error message will be generated.
Limiting the file types / Uploading images with PHP script
In order to limit users to upload only specified file formats in PHP upload file script, you can use the code below. For example you only want to allow images like .jpg, .gif, .png, pdf etc to be uploaded and not .exe or .php or other formats.
This is how you can do it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
<?php
$targetfolder = “testupload/”;
$targetfolder = $targetfolder . basename( $_FILES[‘file’][‘name’]) ;
$ok=1;
$file_type=$_FILES[‘file’][‘type’];
if ($file_type==“application/pdf” || $file_type==“image/gif” || $file_type==“image/jpeg”) {
if(move_uploaded_file($_FILES[‘file’][‘tmp_name’], $targetfolder))
{
echo “The file “. basename( $_FILES[‘file’][‘name’]). ” is uploaded”;
}
else {
echo “Problem uploading file”;
}
}
else {
echo “You may only upload PDFs, JPEGs or GIF files.<br>”;
}
?>
|
As you can see in the script, the $file_type variable contains the file type that is handled in the if condition to limit file type uploads.
The next chapter explains how to increase the limit of file upload size.
Leave A Comment?