« Banner and link rotationCaptcha image verification »

File uploading with PHP Posted in PHP Tutorials | 67 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 experience with millions of other webmasters visiting our website. Contact us for more information how to become a contributor.

67 Replies to "File uploading with PHP"

jayasree September 30, 2014 at 12:08 pm | Reply


khawar October 8, 2014 at 2:48 am


Peter August 1, 2014 at 10:16 am | Reply



Plse help, after the file is uploaded via the above upload script I want it also to be displayed on a php page. The next users upload must be displayed on the same page but next to the previous users etc etc... is this possible?
vipul singh July 1, 2014 at 10:49 am | Reply


while applying the mention code in the tutorial, i'm getting the folllowing error msg..

Notice: Undefined variable: HTTP_POST_FILES in C:\xampp\htdocs\file_upload\upload.php on line 23
File is not uploaded.
Claudia Rogers May 23, 2014 at 12:20 pm | Reply


I have not tried this yet but it looks like its exactly what I need.

I want my registered users to be able to upload video files to specific folders.

Here is what I am thinking"

Step 1-Write an upload.php file for each directory I want files moved to using a varied name.


Step 2-Change the move to part to reflect the directory I want

for example:
move_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'], $folder.$HTTP_POST_FILES['filename']['folder1'])

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

Step 3-Change the form action to the specific upload.php

<form action="upload1.php" method="post" enctype="multipart/form-data">
<form action="upload2.php" method="post" enctype="multipart/form-data">

Am I correct here or is there an easier way?

One other thing:
How do I specify file types?

Thank you

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.......
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 ,

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 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 ~