File uploading with PHP

posted on May 20, 2006, 12:21 am

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" />
</form>

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: 
$HTTP_POST_FILES['filename']['tmp_name']

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.

Comments
sebestiraj posted on June 16, 2010, 3:19 pm
if i want to move the uploading file to my email id what is syntax in moving_uploaded_file please send the syntax

sanjay singh posted on June 6, 2010, 8:07 pm
i want, how a docx file is uploaded in php

Mike posted on March 17, 2010, 5:21 am
now the upload is done...how can i put the image and display at the textfield at form?

cordell posted on January 28, 2010, 8:37 am
should there just be 1 php script or 2 seperate also should the form be in the folder called uploading?

luke dobner posted on June 13, 2009, 3:31 pm
Hi,
I have followed the steps - but when I click the upload button, it redirects to the upload.php - but I get this message;

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/luke9739/public_html/upload.php on line 5


How can I fix this? please help


shweta posted on March 12, 2009, 7:52 am
this is oswam to understand file uploading in php

shweta posted on March 12, 2009, 7:52 am
this is oswam to understand file uploading in php

Kyle posted on October 14, 2008, 8:02 am
I have a very similar code that works just fine:

if ( move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'],
"/files/{$_FILES['uploadFile'] ['name']}") )
{ print '<p> The file has been successfully uploaded </p>';
}
else
{
switch ($_FILES['uploadFile'] ['error'])
{ case 1:
print '<p> The file is bigger than this PHP installation allows</p>';
break;
case 2:
print '<p> The file is bigger than this form allows</p>';
break;
case 3:
print '<p> Only part of the file was uploaded</p>';
break;
case 4:
print '<p> No file was uploaded</p>';
break;
}
}

I hope this helps.


I also have a question. My form (which has a file upload) goes to a process.php file which has some php code in it, which then uses a header to redirect the user to a Thank You page. I would like to display file upload error messages on the Thank You page. Another words, I can't use the above script because I have a PHP page in between my form and my Thank You page. Can I write something into the process.php page? Anybody any ideas?

Thanks,
Kyle
-----------------
Veselin: what you can do is to create an array with all error messages like
$ERROR[0] = 'error 1';
$ERROR[1] = 'error 2';
$ERROR[2] = 'error 3';
and then redirect by passing error ID like error.php?id=1 and on the error message to print the actual error message from ERROR array with id 1

Mark posted on October 6, 2008, 1:10 am
Thanks a ton! I had been beating my head over a ton of other bad scripts that just didn't work! : )

suren posted on August 31, 2008, 11:53 am
i want to upload the files

Pages:   1   2   3   
   
If you have any additions, suggestions or modifications for this example please contact us or use the form below.
   
Your name:
Your email: (email address will not be posted on the web site)
Comment:
Verification