File uploading with PHP
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.
I also get the same exact message. I am running Ubuntu 7.04
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.
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
how can i design a web page with flash plz give some tips.
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.
How can I specify which extensions can be uploaded? I want to allow only .gif and .jpg for example.
Regards, Zoran
can i just pay to get this done for me any takes just email me info@rugeninc.com thanks
this is really great Tutorial
please tell me how can i upload using GET method..
--------------------
Veselin: you cannot upload files using GET method because via get you can only get filename and not its content. Also, when you specify the enctype for your uploading form it applies TO the POST data only.
|