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.
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.
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 |
| Teena Thapa posted on 2008-05-19 10:21:04 |
| here is very intersting side for learning |
| Noah posted on 2008-03-05 19:20:17 |
| I had the same problem. The solution is simple. Simply replace all the ” with " I don't know why the quotes get changed, but that's what was tripping me up... Hope that helps. -Noah |
| Joe posted on 2008-01-04 18:32:52 |
| I also get the same exact message. I am running Ubuntu 7.04 |
| Adam posted on 2007-10-10 17:55:06 |
| 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/silvers/public_html/upload.php on line 5 How can I fix this? Thank's. |
| ANITA SINGH posted on 2007-08-14 14:34:52 |
| How we can find out the image size without upload in any folder or in database. when we browse the image then onclick function we recevied the size of the image ---------------------------- Veselin: to get image size you need to upload the file on your server and then using GD image library you can find out the size |
| suniel posted on 2007-08-01 21:53:32 |
| how can i design a web page with flash plz give some tips. |
| Bikramjit posted on 2007-04-13 23:40:27 |
| i want to upload the file without using html form's default post method. i m sending all data to another php through a popup widow called by javascript how can i include the logic to upload the file at that poped up php page as no post method had been used for passing the data please help as its urgent --------------- Veselin: I do not think you can upload a file without using POST method. In my knowledge uploading only works with POST method. |
| Roland posted on 2007-03-19 12:31:03 |
| Thanks. Nice Script!!! |
| Zoran posted on 2007-03-02 18:09:40 |
| How can I specify which extensions can be uploaded? I want to allow only .gif and .jpg for example. Regards, Zoran |
| stillhustlin posted on 2007-02-24 23:45:07 |
| can i just pay to get this done for me any takes just email me info@rugeninc.com thanks |