simple file upload in php

simple file upload in php

simple file upload in php

In this php tutorial let us check a simple file upload in php. So I’m here going to explain php file upload from basis level. Using php script you can upload and manage your file very easily.

Before explaining to how to write a php script to upload a file. You must have a basic knowledge of its html part. because for uploading a file with help of a browser you to know how to create a html form that will help you to upload a file.

If you want to know about multiple file upload php script check this tutorial.

For creating a html file upload for you can use the code below.

<form action="" enctype="multipart/form-data" method="POST">Choose a file to upload : <input type="file" multiple="multiple" name="file" />
 <input type="submit" /></form>

So by using this form you can create a html form uploader. The form is having, action=”” – in this I put the action as blank. If you give the action of your form as blank the action will be that form itself, because we are going to write the php file uploading script in the same page itself. If you are writing the php file uploading code in a separate page ex (upload.php) you can specific action=”upload.php”. enctype=”multipart/form-data” – For uploading a file type you must set this. THis is because if you are posting a form with some files in it, That form need to be encoded. So this is the html part of file upload. Here when a user click on submit after clicking a file, the form will submit ( here the same page itself ).

Next comes the php part.

For a simple file upload in php we need to create a php script, that will help you to upload the file from a users computer to the website server. So when the form is submitted our php file upload code will start to execute. Before explaining the code there are some thing that you need to keep in mind. 1. For uploading a file to the server you need to specify in which folder do you want to upload the file. 2. When uploading a file you also need to specify which are the file type that are allowed to upload. 3. You also need to specify the max size of the file that a user want to upload. So let us keep moving to php part. Here i’m writing a simple php file upload script, and will expalin in detail after that.


	    $file_dir="uploads";

	    $file_name = $_FILES['file']['name'];

		$file_size =$_FILES['file']['size'];

		$file_tmp_name =$_FILES['file']['tmp_name'];

		$file_type=$_FILES['file']['type'];

		move_uploaded_file($file_tmp_name,"$file_dir/".$file_name);

The above code is explain for uploading a file to your web server. But this is only a basic php file upload code. We need to add so much functionality to this code so that it will be error free. Some of the impotent things in the above code are listed below. $file_dir : the directory where you want to upload the file. $_FILES : This is a HTTP File Upload variables which is used for uploading file. This holds so many information of the file which a user is trying to upload. $_FILES['files']['name'] : contain the name of the file. $_FILES['files']['size'] : will give information about the size of the file. $_FILES['files']['tmp_name'] : give the location where the file is uploaded temporarily uploaded in server. $_FILES['files']['type'] : Give the file type ex jpeg,png,gif,pdf ect… move_uploaded_file : This is th php function that move the file uploaded to the temporary directory to the directory that we are specify. So as we mentioned above, We need to add some more functionality to our simple php file upload script. So that it will have less chance to have any errors. Also we need to implement some security check too. For confirming that a user is uploading only the files that aloud them do upload. So let us move to the advanced part. Here


  if(isset($_FILES['file'])){

	$allowed_file_type = array("pdf","ppt","pptx","xls"."xlxs","doc","docx","jpg", "jpeg", "png", "gif");
	$max_file_size = 2097152;

   $file_dir="uploads";

	$file_name = $_FILES['file']['name'];

	$file_size =$_FILES['file']['size'];

	$file_tmp_name =$_FILES['file']['tmp_name'];

	$file_type=$_FILES['file']['type'];

	$ext = pathinfo($file_name, PATHINFO_EXTENSION);

	if (in_array($ext, $allowed_file_type)) {

	      if($file_size > $max_file_size){

				$fsize=$max_file_size/1048576;
				echo 'File size must be less than '.$fsize.' MB';
				break;

			}

			 if(is_dir($file_dir)==false){

              $status =  mkdir("$file_dir", 0700);

               if($status < 1){

					 echo "unable to create  diractory $file_dir ";
					 break;
                }

		    }

			if(is_dir($file_dir)){

			    move_uploaded_file($file_tmp_name,"$file_dir/".$file_name);

		}

	}

}

So We wrote the more advanced and more secured file uploading php script. But still it is a simple file upload in php. Here Let us check some of the imported things that you keep in mind below. $allowed_file_type = array(“pdf”,”ppt”,”pptx”,”xls”.”xlxs”,”doc”,”docx”,”jpg”, “jpeg”, “png”, “gif”); : we are giving the allowed files extension as an array. So ig you are inly want to upload images using the php file upload script. than you just change the $allowed_file_type array to $allowed_file_type=array(“jpg”, “jpeg”, “png”, “gif”); $max_file_size = 2097152; In this code we are specifying the max file size of the file that a user can upload in bytes. pathinfo($fname, PATHINFO_EXTENSION); This code will give the file extension if (in_array($ext, $allowed_file_type)) line of code is checking whether the user is uploading the allowed files only. if($file_size > $max_file_size) : In this we are checking comparing the uploaded file size with the max file size. if(is_dir($file_dir)==false) : in this lines we are checking whether the specified upload directory exist or not. $status = mkdir(“$file_dir”, 0700); : Here we are creating the file directory if it don’t present in server. So let us put all code together.


  if(isset($_FILES['file'])){

	$allowed_file_type = array("pdf","ppt","pptx","xls"."xlxs","doc","docx","jpg", "jpeg", "png", "gif");
	$max_file_size = 2097152;

   $file_dir="uploads";

	$file_name = $_FILES['file']['name'];

	$file_size =$_FILES['file']['size'];

	$file_tmp_name =$_FILES['file']['tmp_name'];

	$file_type=$_FILES['file']['type'];

	$ext = pathinfo($file_name, PATHINFO_EXTENSION);

	if (in_array($ext, $allowed_file_type)) {

	      if($file_size > $max_file_size){

				$fsize=$max_file_size/1048576;
				echo 'File size must be less than '.$fsize.' MB';
				break;

			}

			 if(is_dir($file_dir)==false){

              $status =  mkdir("$file_dir", 0700);

               if($status < 1){

					 echo "unable to create  diractory $file_dir ";
					 break;
                }

		    }

			if(is_dir($file_dir)){

			    move_uploaded_file($file_tmp_name,"$file_dir/".$file_name);

		}

	}

}
<form action="" enctype="multipart/form-data" method="POST">Choose a file to upload : <input type="file" multiple="multiple" name="file" />
 <input type="submit" /></form>


So from using this script above you can easily create a simple file upload in php.

Download