Uploading with PHP

There are all sorts of cool things you can do with a file upload script – upload banners for a banner rotator, upload avatars for a forum, upload zips for a download site, the list goes on.

You may not have realized it, but uploading in PHP is very easy. Here we’ll go over the two basic parts of uploading, the upload form, and the upload script.

Upload Form

<form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
    <input type="hidden" name="action" value="submit">

This is your basic upload form. Some key things to point out: MAX_FILE_SIZE is what sets the max size. Note that if the PHP ini has a smaller max file size, the ini size is used. enctype If the data encoding type is not defined in this way, it just won’t work.

Upload Script

if ($action == "submit") {
$uploaddir = '/full/path/to/final/folder/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
   echo "Success: File uploaded.";
} else {
   echo "Error: File not uploaded.";

As you can see, PHP handles the uploading of the file automatically – it is just up to you to move the file from the temporary location to the final destination using move_uploaded_file.