DARTHTAMPON
10-10-2004, 11:01 PM
File upload how-to
Ok, so I spent the last couple of days trying to build a file upload program and finally figured everything out so I figured why not post a how-to.
The beginning, HTML form tags
================================
<form name="name" action="<?php echo $_SERVER['../PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input type="file" name="filename” size="70">
</form>
First: Don’t call your form name, it’s completely useless because it tells you nothing about the form. If you insist on this leave the name part blank and when validating in javascript use document.forms[0] instead.
Second: action="<?php echo $_SERVER['../PHP_SELF']; ?>" this piece of code inserts the location of the current document, something like /home/username/main_html/yourfile.php. Always try to use this form instead of actually writing it out, by doing so when your web-host boots you, you can reupload all your files to a different server and they will still work.
Third: method="post" I believe you can use post or get with file uploading. (not sure on this (some one post and correct if I am wrong)) Unless you have a very good reason though use post to keep things simple.
Forth: enctype="multipart/form-data” make sure you specify this, if you don’t it will not work right.
Fifth: <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> This must go before the input tag for the file. It specifies the max size for the file you wish to upload. This helps to reduce bandwidth on your server for people trying to upload files that are to large. If you have more than 1 file you are trying to upload make MAX_FILE_SIZE the max of the largest file, you cannot readjust this value as you go (this should be changes)
Sixth: <input type="file" name="filename” size="70"> This gives you a textbox and browse button. The size="70” tells the browser how large the textbox should be.
Ok now that we figured out the easy part on to the harder (but still quite easy part)
The end, PHP code
==============================
if( !(strlen($_FILES[‘filename’]['name']) < 1 ))
{
$file[0] = $_FILES[‘filename’]['name'];
$file[1] = $_FILES[‘filename’]['type'];
$file[2] = $_FILES[‘filename’]['size'];
$file[3] = $_FILES[‘filename’]['tmp_name'];
$file[4] = $_FILES[‘filename’]['error'];
move_uploaded_file($file[3], “upload/”.$file[0])
}
First: if( !(strlen($_FILES[‘filename’]['name']) < 1 )) this line checks to see if a file was uploaded or not, if you want the user to know that they forgot to upload a file throw in an else.
Second: $file[0] = $_FILES[‘filename’]['name']; this line of code retrieves the name of the file on the client side. So if you upload a file called file.jpg, $file[0] will equal file.jpg.
Third: $file[1] = $_FILES[‘filename’]['type']; This line of code returns the type of file uploaded, this will look like image/gif, image/jpeg, image/jpeg etc. When checking to see if a file is a jpeg check for both the image/jpeg and image/pjpeg. Different browsers use one or the other. IE uses image/pjpeg.
Forth: $file[2] = $_FILES[‘filename’]['size']; This line of code returns the size of the file in bytes, this can be used to check to see if files are of correct size. Always use this to double check file sizes because MAX_FILE_SIZE in HTML can be tricked to allow files that are larger to be uploaded, whereas the php side can not be tricked.
Fifth: $file[3] = $_FILES[‘filename’]['tmp_name']; This line of code returns the temp name of the uploaded file on the server
Sixth: $file[4] = $_FILES[‘filename’]['error']; this line of code returns any errors that were reported during the file upload.
Seventh: move_uploaded_file($file[3], “upload/”.$file[0]) this line of code moves the uploaded file from the temp directory to wherever you want to put it. Actule cade not using variable will look like this move_uploaded_file($_FILES[‘filename’]['tmp_name'], “upload/”.$_FILES[‘filename’]['name']); this will move the file to “location of your document”/upload with a file name of what ever was read from the client side.
That does it everything you will ever need to know about simple file uploading in a 2 page document. I will add info about error reporting and other things if I ever get around to it.
Please feel free to post comments, fixes or random nothingness (to try and build your post count ;) )
cheers
Ok, so I spent the last couple of days trying to build a file upload program and finally figured everything out so I figured why not post a how-to.
The beginning, HTML form tags
================================
<form name="name" action="<?php echo $_SERVER['../PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<input type="file" name="filename” size="70">
</form>
First: Don’t call your form name, it’s completely useless because it tells you nothing about the form. If you insist on this leave the name part blank and when validating in javascript use document.forms[0] instead.
Second: action="<?php echo $_SERVER['../PHP_SELF']; ?>" this piece of code inserts the location of the current document, something like /home/username/main_html/yourfile.php. Always try to use this form instead of actually writing it out, by doing so when your web-host boots you, you can reupload all your files to a different server and they will still work.
Third: method="post" I believe you can use post or get with file uploading. (not sure on this (some one post and correct if I am wrong)) Unless you have a very good reason though use post to keep things simple.
Forth: enctype="multipart/form-data” make sure you specify this, if you don’t it will not work right.
Fifth: <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> This must go before the input tag for the file. It specifies the max size for the file you wish to upload. This helps to reduce bandwidth on your server for people trying to upload files that are to large. If you have more than 1 file you are trying to upload make MAX_FILE_SIZE the max of the largest file, you cannot readjust this value as you go (this should be changes)
Sixth: <input type="file" name="filename” size="70"> This gives you a textbox and browse button. The size="70” tells the browser how large the textbox should be.
Ok now that we figured out the easy part on to the harder (but still quite easy part)
The end, PHP code
==============================
if( !(strlen($_FILES[‘filename’]['name']) < 1 ))
{
$file[0] = $_FILES[‘filename’]['name'];
$file[1] = $_FILES[‘filename’]['type'];
$file[2] = $_FILES[‘filename’]['size'];
$file[3] = $_FILES[‘filename’]['tmp_name'];
$file[4] = $_FILES[‘filename’]['error'];
move_uploaded_file($file[3], “upload/”.$file[0])
}
First: if( !(strlen($_FILES[‘filename’]['name']) < 1 )) this line checks to see if a file was uploaded or not, if you want the user to know that they forgot to upload a file throw in an else.
Second: $file[0] = $_FILES[‘filename’]['name']; this line of code retrieves the name of the file on the client side. So if you upload a file called file.jpg, $file[0] will equal file.jpg.
Third: $file[1] = $_FILES[‘filename’]['type']; This line of code returns the type of file uploaded, this will look like image/gif, image/jpeg, image/jpeg etc. When checking to see if a file is a jpeg check for both the image/jpeg and image/pjpeg. Different browsers use one or the other. IE uses image/pjpeg.
Forth: $file[2] = $_FILES[‘filename’]['size']; This line of code returns the size of the file in bytes, this can be used to check to see if files are of correct size. Always use this to double check file sizes because MAX_FILE_SIZE in HTML can be tricked to allow files that are larger to be uploaded, whereas the php side can not be tricked.
Fifth: $file[3] = $_FILES[‘filename’]['tmp_name']; This line of code returns the temp name of the uploaded file on the server
Sixth: $file[4] = $_FILES[‘filename’]['error']; this line of code returns any errors that were reported during the file upload.
Seventh: move_uploaded_file($file[3], “upload/”.$file[0]) this line of code moves the uploaded file from the temp directory to wherever you want to put it. Actule cade not using variable will look like this move_uploaded_file($_FILES[‘filename’]['tmp_name'], “upload/”.$_FILES[‘filename’]['name']); this will move the file to “location of your document”/upload with a file name of what ever was read from the client side.
That does it everything you will ever need to know about simple file uploading in a 2 page document. I will add info about error reporting and other things if I ever get around to it.
Please feel free to post comments, fixes or random nothingness (to try and build your post count ;) )
cheers