Single Files Upload with PHP
In this tutorial we will make simple php file upload system with a verification for file extension and size, thus making it a secure way to upload files.
You can use this for Uploading image's ,PDF's, Doc any file types make sure you change the necessary parts in the script.
You can also see a tutorial on Multiple file upload with PHP and Jquery Ajax File Upload.
To get the extension we will use the name as it will have the extension, to extract it we will use PHP explode() & use end(). There won't be any problem even if the file name has a dot in it.
To move the file from the tmp_name to another location we will be using move_uploaded_file(), here we will move it to images directory, make sure the directory exist, as move_uploaded_file() cannot create a directory.
Now let's take a look at the full code once again.
You can use this for Uploading image's ,PDF's, Doc any file types make sure you change the necessary parts in the script.
You can also see a tutorial on Multiple file upload with PHP and Jquery Ajax File Upload.
THE HTML
This is a simple HTML form containing interface for php file Upload system.
<html> <head> <title>Single Files Upload with PHP</title> </head> <body> <table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td><strong>Single Files Upload with PHP</strong></td> </tr> <tr> <td>Select file <input name="file" type="file" size="50" /></td> </tr> <tr> <td align="center"><input type="submit" name="Submit" value="Upload" /></td> </tr> </table> </td> </form> </tr> </table> </body> </html>Make sure to make add enctype="multipart/form-data" to form and type="file" for the input.
PHP
We will use isset($_FILES[]) to make sure some file is selected and then we will proceed for the upload.
$_FILES[' '] is an array with the file information. $_FILES[] contain temporary name, size, type, name and error information of uploaded file in an array.
$_FILES description
This is the output when sunset.jpg was uploaded. The [name] => sunset.jpg is the name of the file. [type] => image/jpeg is the type of the file, [tmp_name] => C:\wamp\tmp\php26CPE.tmp tmp_name is the temporary location where the file is uploaded , in what ever server you are running on, We will use move function to move the file to our desired location later. [error] => 0 Its the error variable, we are not using that in this tutorial, [size] => 21654 and the last one is the size of the file, we will use it to make sure that the files above the a certain limit is not uploaded.
if(isset($_FILES['file'])){ $file_name = $_FILES['file']['name']; $file_size =$_FILES['file']['size']; $file_tmp =$_FILES['file']['tmp_name']; $file_type=$_FILES['file']['type']; }Now to get started with verification.
$extensions = array("jpeg","jpg","png");In in example we are uploading an image so we need to allow the image extensions. You can add the appropriate extensions that you need.
To get the extension we will use the name as it will have the extension, to extract it we will use PHP explode() & use end(). There won't be any problem even if the file name has a dot in it.
$file_ext=explode('.',$_FILES['file']['name']) ;
$file_ext=end($file_ext);
Extensions can also be in UPPER case or LOWER case to overcome the problem we will get them converted into lower case or upper case as you mentioned in the $extensions array.
$file_ext=strtolower(end(explode('.',$_FILES['file']['name'])));With in_array() you can get it checked extension is present in allowed extension
if(in_array($file_ext,$extensions ) === false){
$errors[]="extension not allowed";
}
We will make an array to store errors and check if the error is empty or not to confirm the upload or echo out the error at the end. To check for size we can use $file_size to check but, make sure that the size is in bytes.
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
Now we have done with the verification part. Now lets move the uploaded file to another folder to user that file in future and display a confirmation message.To move the file from the tmp_name to another location we will be using move_uploaded_file(), here we will move it to images directory, make sure the directory exist, as move_uploaded_file() cannot create a directory.
if(empty($errors)==true){
move_uploaded_file($file_tmp,"upload/".$file_name);
echo "Your file uploaded successfully!!!";
}else{
print_r($errors[]);
}
PHP SINGLE FILE UPLOAD WITH PHP
Now let's take a look at the full code once again.
<?php if(isset($_FILES['file'])){ $errors= array(); $file_name = $_FILES['file']['name']; $file_size =$_FILES['file']['size']; $file_tmp =$_FILES['file']['tmp_name']; $file_type=$_FILES['file']['type']; $file_ext=strtolower(end(explode('.',$_FILES['file']['name']))); $extensions = array("jpeg","jpg","png"); if(in_array($file_ext,$extensions )=== false){ $errors[]="Extension not allowed, please choose a JPEG or PNG file."; } if($file_size > 2097152){ $errors[]='File size must be less than 2 MB'; } if(empty($errors)==true){ move_uploaded_file($file_tmp,"upload/".$file_name); echo "Your file uploaded successfully!!!"; }else{ print_r($errors); } } ?> <html> <head> <title>Single Files Upload with PHP</title> </head> <body> <table width="500" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"> <tr> <form action="" method="post" enctype="multipart/form-data" name="form1" id="form1"> <td> <table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"> <tr> <td><strong>Single Files Upload with PHP</strong></td> </tr> <tr> <td>Select file <input name="file" type="file" size="50" /></td> </tr> <tr> <td align="center"><input type="submit" name="Submit" value="Upload" /></td> </tr> </table> </td> </form> </tr> </table> </body> </html>Hope this php tutorial is useful for you. Keep following Php Point for more Codes.