Uploading Files in php

Uploading files to a web server is a subject area that seems daunting to many people, but it actually couldn’t be much easier. All you need to do to upload a file from a form is choose a special type of encoding called multipart/form-data and your browser will handle the rest. To see how this works, type in the following program and save it as upload.php. When you run it, you’ll see a form in your browser that lets you upload a file of your choice.

<?php // index.php
echo <<<_END
<html><head><title>PHP Form Upload</title></head><body>
<form method='post' action='index.php' enctype='multipart/form-data'>
Select File: <input type='file' name='filename' size='10' />
<input type='submit' value='Upload' />
</form>
_END;
if ($_FILES)
{
	$name = $_FILES['filename']['name'];
	move_uploaded_file($_FILES['filename']['tmp_name'], $name);
	echo "Uploaded image '$name'<br /><img src='$name' />";
}
echo "</body></html>";
?>

Let’s examine this program a section at a time. The first line of the multiline echo statement starts an HTML document, displays the title, and then starts the document’s body.
 
Next we come to the form that selects the POST method of form submission, sets the target for posted data to the program upload.php (the program itself), and tells the web browser that the data posted should be encoded using the content type of multipart/form-data.
 
With the form set up, the next lines display the prompt “Select File:” and then request two inputs. The first input being asked for is a file, which is done by using an input type of file and a name of filename, and the input field has a width of 10 characters. The second requested input is just a Submit button that is given the label “Upload” (replacing the default button text of “Submit Query”). And then the form is closed. This short program shows a common technique in web programming in which a single program is called twice: once when the user first visits a page, and again when the user
presses the Submit button.
 
The PHP code to receive the uploaded data is fairly simple, because all uploaded files are placed into the associative system array $_FILES. Therefore a quick check to see whether $_FILES has anything in it is sufficient to determine whether the user has uploaded a file. This is done with the statement if ($_FILES).
 
The first time the user visits the page, before uploading a file, $_FILES is empty, so the program skips this block of code. When the user uploads a file, the program runs again and discovers an element in the $_FILES array.
 
Once the program realizes that a file was uploaded, the actual name, as read from the uploading computer, is retrieved and placed into the variable $name. Now all that’s necessary is to move the file from the temporary location in which PHP stored the uploaded file to a more permanent one. This is done using the move_uploaded_file function, passing it the original name of the file, with which it is saved to the current directory.