PHP Code
The file is available by referencing the superglobal $_FILES array. The first index is the form's input name and the second index can be either "name", "type", "size", "tmp_name" or "error".
$_FILES["file"]["name"] holds the name of the file uploaded from the client
$_FILES["file"]["type"] holds the mimetype of the uploaded file
$_FILES["file"]["size"] holds the size in bytes of the uploaded file
$_FILES["file"]["tmp_name"] holds the name of the temporary copy of the file stored on the server
$_FILES["file"]["error"] holds any error code resulting from the file transfer
For a listing of possible error codes, see
www.php.net/manual/en/features...rrors.php.
Example code using $_FILES might look like this:
<?php
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
echo "Uploading " . $_FILES["file"]["name"];
echo " (" . $_FILES["file"]["type"] . ", ";
echo ceil($_FILES["file"]["size"] / 1024) . " Kb).<br />";
echo "File is temporarily stored as " . $_FILES["file"]["tmp_name"];
?>
However, you will want to keep in mind some of the potential hazards of allowing files to be uploaded to your server... be sure to use type, size and error in determining how to process the file.
<?php
if (($_FILES["file"]["type"] == "image/gif") &&
($_FILES["file"]["size"] < 15000)) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
echo "Uploading " . $_FILES["file"]["name"];
echo " (" . $_FILES["file"]["type"] . ", ";
echo ceil($_FILES["file"]["size"] / 1024) . " Kb).<br />";
echo "File is temporarily stored as ". $_FILES["file"]["tmp_name"];
} else
echo "Sorry, we only accept .GIF images under 15Kb for upload.";
?>
Since the temporary file will be deleted once the script is done processing and the HTTP connection closes, our next task is to copy the temporary file to a safe location. For this we have the function move_uploaded_file.
Source:http://codewalkers.com/tutorials/48/2.html