FAQ: How to upload files in php?

Uploading files in web applications is a common practice. Thanks to php which makes it simple to implement.

This post will present a simple example of how to upload files using php.

Before any code, first let us understand the file upload process .

User is displayed a page with a file select box. She then selects a file and clicks on submit button. The form is submitted now and goes to a php script on the server. There a special array $_FILES gets populated. Similar to $_GET and $_POST, $_FILES contains information about uplaoded files. File information is then extracted from this array and php function move_uploaded_file is used to save file on the server.

Create a new file called upload.php and copy the following code in 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
36
37
<html>
<head>File upload example</head>
<body>
<form enctype="multipart/form-data" action="" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="300000" />
<input type="file" name="uploadFile"/>
<br/>
<input type="submit" name="upload" value="Upload File"/>
</form>
</body>
</html>
 
<?php
print_r($_FILES); 
 
if(isset($_POST['upload']))
{
	$folder = 'path/to/folder/';
	$filName = $_FILES['uploadFile']['name'];
	$uploadedFile = $folder.$filName;
	if(is_uploaded_file($_FILES['uploadFile']['tmp_name']))
	{
		 if(move_uploaded_file($_FILES['uploadFile']['tmp_name'],$uploadedFile))
		 {
			echo 'File uploaded';
		 }
		 else
		{
			echo 'Failed to upload file';
		}
	}
	else
	{
		echo 'error';
	}
}
?>

We created a form with a hidden field, a file select box and a submit button. After that there is php code which will be executed upon submitting the form.

There are some important points worth noting in the html.

Now the php code after the user selects a file and clicks submit. As mentioned earlier $_FILES will be filled with information about uploaded file and the selected file will be stored in tmp directory specified in php.ini until we save it to desired location by php code.

Since the name of file select box was uploadFile, $_FILES will contain an associative array with key being uploadFile.

Line 14 will print the contents of $_FILES which look like this:

Array
(
    [uploadFile] => Array
        (
            [name] => originalFile.txt
            [type] => application/octet-stream
            [tmp_name] => path/to/tmp_dir/filename.tmp
            [error] => 0
            [size] => 79
        )
)
)

This array has 5 elements. name is the original name of uploaded file.type is the mime type of file.tmp_name is the name given to the uploaded file in tmp directory. error is the error code returned while uploading the file. 0 means success while uploading and size is the size of uploaded file in bytes.

In line 20, we construct a string which contains the desired path where you wish to upload the file.

Line 21 has the function is_uploaded_file which checks if the file was really uploaded through http post or not.

Now comes the function move_uploaded_file which will move the file from tmp directory to desired path. It accepts 2 parameters: name of the file to upload and the destination path. It returns true on success, false otherwise. In our case first parameter will be the $_FILES['uploadFile']['tmp_name'] and second will be $uploadedFile.

Successful call to move_uploaded_file will move the file from tmp directory to desired path.

Here is the list of possible error code taken from the PHP Manual

0- No error, file uploaded.
1- File size is greter then upload_max_filesize value in php.ini.
2- File size is greter then MAX_FILE_SIZE value in HTML form.
3- File partially uploaded.
4- No file uploaded.
6- No temporary folder.
7- Failed writing file to disk.
8- File upload stopped.

Comments are most welcome in case of any problem.

Related Posts

FAQ, php

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Comments

One Response to “FAQ: How to upload files in php?”

Leave Comment

(required)

(required)


9,573 spam comments
blocked by
Akismet