Site Navigation:

Home // Tutorials // PHP // Uploading a File

Uploading a File

1
Many times, a programmer will want to let the user upload a file to the server, like on deviantart.com 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">
<html>
<head>
<title>Title here!</title>
</head>
<body>
<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!">
</form>
</body>
</html>


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.

<?php

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 z-team@attbi.com or contact me on www.neverside.com My user name is Adman.
spacer

This Tutorial was by Adman, Please post any questions in the forum. Thank you.
BACK TO TUTORIALS



Home Tutorials Free Stuff About Gallery Forum Links Contact

Copyright © Robouk 2001.

Dreamweaver®, Flash® and Photoshop® are either registered trademarks or trademarks of Adobe Systems Incorporated in the United States and/or other countries.
Hosted by Pixelcode - web design tutorials.