A Simple File Uploader in PHP

Hi, as upload is a necessity today, many would need this type of uploader for simple use.

1st create a php file named upload.php

Now put the below content in it and run it in your local server.

enctype=”multipart/form-data” is mandatory for file uploads in the HTML Form.

<!– put all in upload.php file –>
<form action=”upload.php” method=”post” enctype=”multipart/form-data”>
Browse a File to Upload: <i>file must be 1MB or less. (1048576 bytes)</i><br>
<input type=”file” name=”filetoupload”><br>
<input type=”hidden” name=”MAX_FILE_SIZE” value=”<?echo $size_bytes; ?>”>
<input type=”Submit” value=”Upload File”>

/*  Description —————————————————–
The Super Global Variable $_FILES is used in PHP 4.x.x.
$_FILES[‘upload’][‘size’] ==> Get the Size of the File in Bytes.
$_FILES[‘upload’][‘tmp_name’] ==> Returns the Temporary Name of the File.
$_FILES[‘upload’][‘name’] ==> Returns the Actual Name of the File.
$_FILES[‘upload’][‘type’] ==> Returns the Type of the File.

So if I filetoupload the file ‘test.doc’, the $_FILES[‘upload’][‘name’]
would be ‘phptut.doc’ and $_FILES[‘upload’][‘type’] would be ‘application/msword’.
// this is the upload dir where files will go.
//Don’t remove the /
//Chmod it (777)
$upload_dir = “images/”;   //change to whatever you want.
// files less than 1MB
$size_bytes = 1048576; //bytes  will be uploaded
//check if the directory exist or not.
if (!is_dir(“$upload_dir”)) {
die (“The directory <b>($upload_dir)</b> doesn’t exist”);
//check if the directory is writable.
if (!is_writeable(“$upload_dir”)){
die (“The directory <b>($upload_dir)</b> is NOT writable, Please Chmod (777)”);

//Check first if a file has been selected
//is_filetoupload_file(‘filename’) returns true if
//a file was filetoupload via HTTP POST. Returns false otherwise.
if (is_uploaded_file($_FILES[‘filetoupload’][‘tmp_name’]))

//Get the Size of the File
$size = $_FILES[‘filetoupload’][‘size’];
//Make sure that $size is less than 1MB (1000000 bytes)
if ($size > $size_bytes)
echo “File Too Large. Please try again.”;

// $filename will hold the value of the file name submetted from the form.
$filename =  $_FILES[‘filetoupload’][‘name’];
// Check if file is Already EXISTS.
echo “Oops! The file named <b>$filename </b>already exists”;

//Move the File to the Directory of your choice
//move_filetoupload_file(‘filename’,’destination’) Moves an filetoupload file to a new location.
if (move_uploaded_file($_FILES[‘filetoupload’][‘tmp_name’],$upload_dir.$filename)) {

//tell the user that the file has been filetoupload
echo “File (<a href=$upload_dir$filename>$filename</a>) uploaded!”;

//Print error
echo “There was a problem moving your file”;



Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s