« Banner and link rotationCaptcha image verification »

File uploading with PHP Posted in PHP Tutorials | 62 Comments
PHP allows you to upload files using a simple HTML form on your web page directly on the server. There are few things that you need to do to make this working – page with web form, uploading script, folder on your server with permission set to be able to write in it.

Lets start with the form. Below you can see a basic web form for uploading a file.

<form action="upload.php" method="post" enctype="multipart/form-data">
File: <input type="file" name="filename" />
<input type="submit" value="Upload" />

The most important about the form is that you should make it uses POST and not GET method and also you should use enctype="multipart/form-data" so it knows that a file will be transferred.
Now we need to make a folder (names 'uploading') on our web server and set its permissions to 777 so the upload.php script that we will do is able to write the file in it. In rare cases depending on your server configuration you may not need to do this. We should also know the absolute path on the server for that folder (example: /home/username/www/uploading/)
Now we are ready to make our upload.php script.

$folder = “/home/username/www/uploading/”;
if (is_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'])) {
if (move_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'], $folder.$HTTP_POST_FILES['filename']['name'])) {
Echo “File uploaded”;
} else {
Echo “File not moved to destination folder. Check permissions”;
} else {
Echo “File is not uploaded.”;

Now, I will explain what the above code do. When, a file is uploaded it is first given a temp filename and then put in the temp folder of your web server. That temp filename is accessible using the global $HTTP_POST_FILES array variable. On our web form we have our browse field named “filename” (<input type="file" name="filename" />), so the name of that temp file is:

The real name of the file being uploaded is stored in another variable called $HTTP_POST_FILES['filename']['name']. As you can see this just another array element named “name” in the $HTTP_POST_FILES['filename'] array.

Now after having that file upload in our web server temp folder we need to move it to the folder specified $folder=“/home/username/www/uploading/”. This is done using the move_uploaded_file() function.

move_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'], $folder.$HTTP_POST_FILES['filename']['name'])

the first parameter that it takes is the temp filename and the second parameter is the destination folder and filename. If it successfully moves the temp file to the folder that we want it returns TRUE and we made it print “File uploaded” message on the screen.

Do you know PHP / HTML / CSS / JS well?

Write tutorial on a topic you are good in and become a trusted PHP jabber! Share your knowledge with thousands of webmasters and we will reward you for your generosity by giving you bonus points which you can use as a voucher to buy any of our commercial products. Read more about our reward program.

62 Replies to "File uploading with PHP"

Daniel April 10, 2014 at 7:56 am | Reply


So when I do it. every time it yells at me saying file is not uploaded. What should I change in the code to make it work?
Jamil February 8, 2014 at 10:09 pm | Reply


Consider having someone proof-read articles before uploading them. It's important to make your content look professional.
rokad naina January 29, 2014 at 7:05 pm | Reply


for php website solution.......
pooja December 28, 2013 at 10:13 am | Reply


jams December 7, 2013 at 5:01 pm | Reply


how we send input type="file" path's to php page via Ajax ???

 $("#uploadBtn1").change(function (){
var fileExtension = ['jpeg', 'jpg', 'png', 'gif', 'bmp'];
if ($.inArray($(this).val().split('.').pop().toLowerCase(), fileExtension) == -1)
$("#val1").html("Only '.jpeg','.jpg', '.png', '.gif', '.bmp' formats are allowed.");
else {
jams December 7, 2013 at 5:03 pm


i get File path in Php page but how i use $_File['uploadBtn1']['name'] and other such tags ???
kavimani September 7, 2013 at 9:34 am | Reply


hi i want coding for download the upload file in php without using database
Sochenda August 14, 2013 at 11:51 am | Reply


I want to upload mp3 but store path in database ,
samar August 4, 2013 at 5:57 pm | Reply


Parse error: syntax error, unexpected 'not' (T_STRING), expecting ',' or ';' in E:\xampp\htdocs\mpo\admin\upload_file.php on line 13
missy July 25, 2013 at 4:55 am | Reply


<title>PHP File Upload Example</title>
<form enctype="multipart/form-data" method="post" action="uploadFile.php">
<input type="file" name="fileToUpload" /><br />
<input type="submit" value="Upload File" />


echo "<table border="1">";
echo "<tr><td>Client Filename: </td>
<td>" . $_FILES["fileToUpload"]["name"] . "</td></tr>";
echo "<tr><td>File Type: </td>
<td>" . $_FILES["fileToUpload"]["type"] . "</td></tr>";
echo "<tr><td>File Size: </td>
<td>" . ($_FILES["fileToUpload"]["size"] / 1024) . " Kb</td></tr>";
echo "<tr><td>Name of Temp File: </td>
<td>" . $_FILES["fileToUpload"]["tmp_name"] . "</td></tr>";
echo "</table>";

if ($_FILES["fileToUpload"]["error"] > 0)
echo "Apologies, an error has occurred.";
echo "Error Code: " . $_FILES["fileToUpload"]["error"];

"C:/xampp/htdocs/upload/" . $_FILES["fileToUpload"]["name"]);


Sheila July 1, 2013 at 6:07 am | Reply


i got an error

Parse error: syntax error, unexpected 'uploaded”' (T_STRING) in C:\xampp\htdocs\uplod.php on line 5
mansab October 6, 2013 at 4:28 pm


in else condition you put this code

move_uploaded_file($path,$destination)?"file moved":"file not moved";

Please be polite and helpful and do not spam or offend others. We promise you will be treated the same way.

Log in your free account or if you still haven't joined our Webmaster Community Reward Program, you can create your free account now.

Posting tip:
if you use code in your comments please put it in these tags [php], [sql], [css], [js]
PHP code example: [php] echo date("Y-m-d"); [/php]

Thank you,
~ PHPJabbers team ~