PHP File Upload


This tutorial explains how to upload files by using PHP scripts and HTML form.

First we need to create HTML form for uploading files,


      <form action="fileupload.php" method="post" 
       <input type="file" name="file_upload" /> 
      <input type="submit" value="UPLOAD" />

save as upload.html.

Please notice following things while creating HTML form for uploading files.

The form element consists of following attributes :

  • method = “post” ,method should be post
  • enctype =”multipart/form-data” ,this represents the content type of the submitting data to server.

The file type must be file ie,

  • type=”file”

Next step is to write the PHP code to upload the file. Create a new php file save with filename as fileupload.php.

ie, when the user browse the file and submit the form, the data send to fileupload.php page.

The page contains PHP scripts to upload the files:

   if(isset($_POST['file_upload'])) {   
          $file_name     = $_FILES["file_upload"]["name"];          $file_tmpname  = $_FILES["file_upload"]["tmp_name"];                      $file_type     = $_FILES["file_upload"]["type"];
         $file_size     = $_FILES["file_upload"]["size"];          $path          = "/specify the path to move the file/";                            if(move_uploaded_file($file_tempname,$path.$file_name))               echo "File Uploaded";
               echo"failed to upload"; } ?>

move_uploaded_file(source,destination) function is move the uploaded file from source to destination path.

source : specifies temporary name of uploaded file

destination : specifies where we want to move the uploaded file.

Filter: File type

// Only accepted pdf,txt,doc files
if($file_type ==”application/pdf” || $file_type==”application/txt ||$file_type==”application/doc”){}

// for images, the following code allowed to upload image files with Jpeg,jpg,png extension.
if($file_type==”image/jpeg” ||  $file_type==”image/jpg” || $file_type==”image/png”){}

Filter:File Size

if($file_size < 1000){
echo "File size exceeds";

File already exists

check if the file already exists in the uploading folder, then the file is uploading with a random name.

if(in_array($ext,$extarr)) {
echo $orgname=md5(rand(1,100)).ext;


FInally include all the validation in our fileopload.php. Then the php page look likes,

    $file_name     = $_FILES["file_upload"]["name"];
    $file_tmpname  = $_FILES["file_upload"]["tmp_name"];
    $file_type     = $_FILES["file_upload"]["type"]."</br>";
    $file_size     = $_FILES["file_upload"]["size"];
    $path          = "/specify the path to move the file/";
    if($file_type == "application/pdf" || $file_type == 
       // only allow pdf and text files
       if($file_size <="25000"){
          // checking size of uploaded files
          if(in_array($ext,$extarr)) {
             //random name
                $orgname = md5(rand(1,100)).ext;
                 echo "File uploaded";
                echo"Something went wrong";


Leave a Reply

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