PHP File Uploader

In this quick tutorial, I am going to give away my well honed simple script for uploading files with PHP. The code below should have everything you need to start your own custom file uploaded. Since I don’t know what types of files you need an uploader for, I made this version work with any type of file. I did demonstrate how to get the file type, so adding code to limit your uploads to images, text files etc, would be simple. Here is the tested and working PHP code:

<!DOCTYPE html>
<html>
<head>
<title>Jafty file uploader</title>
<meta name=”robots” content=”noindex”>
</head>
<body>

<?php
if(isset($_POST[‘ssd’])){
?>

Processing file upload….<br />

<?php
if($_FILES[“file”][“error”] > 0){
echo “Error: ” . $_FILES[“file”][“error”] . “<br />”;
}else{
echo “Upload: ” . $_FILES[“file”][“name”] . “<br />”;
echo “Type: ” . $_FILES[“file”][“type”] . “<br />”;
echo “Size: ” . ($_FILES[“file”][“size”] / 1024) . ” kB<br />”;
echo “Temp Stored in: ” . $_FILES[“file”][“tmp_name”]. “<br />”;
//upload file
    if(move_uploaded_file($_FILES[“file”][“tmp_name”],
    “uploader_files/” . $_FILES[“file”][“name”])){
    echo “File uploaded successfully!<br />”;
    }else{
    echo “<p style=’color:red’>ERROR! File did NOT upload. Check folder permissions…</p>”;
    }
//store file name/location in variable:
$uploadpath = “uploader_files/” . $_FILES[“file”][“name”];
echo “Stored in: ” . $uploadpath . “<br />”;
echo “<a href=’http://jafty.com/blog/$uploadpath’>$uploadpath</a>”;
}//end if/else
}else{//else if file upload form wasn’t submitted, show upload form:
?>
<h1>Jafty File Uploader</h1>
<p>Developed by Ian L. of Jafty.com</p>
<br />
<form action=”” method=”post” enctype=”multipart/form-data”>
Select file to upload<br />
<input type=”file” name=”file” id=”file”><br />
<input type=”submit” id=”ssb” name=”ssd” value=”Continue” />
</form>
<?php
}//end else file upload form not submitted so show it.
?>
</body>            
</html>   

To implement this code, simply copy and paste the above PHP code into a file and give it a name and a .php extension. Modify any file paths and upload it to your server. Then create a new directory named uploader_files in the same directory as the .php file was uploaded and give it proper permissions such as 755. Then go to the proper web address in your browser and test. It should work with only minor modifications for some instances. What’s cool is, this is usually where I tell you to change all the fancy single and double quotes to regular ones in notepad, but now you don’t have to any more if you copied and pasted your code because I found the secret to stopping WordPress’ so called “smart quotes” as you can see if you look closely, both open and close quotes are the same as they should be to keep things nice and easy for you, the reader.

Summary

Note that in the top page of the file there is a no-index tag to stop search engines from indexing this page. This is in case you wish to use this file uploader as part of a CMS or another backend type script for your site. If not, remove that to have your page indexed in search engines. Add some type and other field verification if you plan to let the public use this, otherwise, it’s good to go.

Leave a Reply

Your email address will not be published. Required fields are marked *