PHP Tutorials

File Upload

Learn how to upload files to your site right from your browser...
Many times, a programmer will want to let the user upload a file to the server, like on when they submit there work. This quick tutorial will show you how to upload a file using php.

First we want an html page that lets the user select the file from there computer.

<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<title>Title here!</title>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000">
Select a file to upload! <input type="file" name="userfile"><br>
<input type="submit" value="Upload!">

Save that file as upload.htm Notice the MAX_FILE_SIZE hidden input. We set that number in bytes, as the largest file that can be uploaded. In this case it is one megabtye. Now we want to create the upload.php file that will actually upload the file.
if(!(copy($_FILES['userfile']['tmp_name'], "Upload/" . $_FILES['userfile']['name']))) die("Cannot upload files.");
echo "Upload Complete!";

Less complicated then you thought huh. :) This will only work in version 4.2.0 or greater. If you are running an older veresion replace $_FILES with $HTTP_POST_FILES. When we upload the file it is just a copy. Copy The user file with the name it has on the users computer to the direcrtory Upload/ and call the file it's name. If the upload doesn't work, it dies, else the user sees upload complete. Now go forth and upload!

I hope that my tutorial was easy to follow and clear. If you have any questions you can email me at or contact me on My user name is Adman.
This tutorial was by Adman, brought to you by Robouk, please post any questions in the forum. Thank you.