In today’s session we will see how to upload file in PHP.  Using HTML form and PHP script, you can allow users to upload data on server. Initially we will see uploading in temporary directory and then relocating it into destination.

How to upload file in PHP

Firstly we will create a HTML form that can handle upload and be sure that you don’t forget to include enctype=”multipart/form-data” attribute in FORM field else it won’t work.

HTML CODE :

<form action="do_upload.php" enctype="multipart/form-data" method="POST">
    
    <input type="hidden" name="MAX_FILE_SIZE" value="51200"/>
    <p><strong>File to Upload:</strong> <input type="file" name="fileupload"/></p>
    <p><input type="submit" name="submit" value="upload!"></p>

</form>

Output :

PHP FILE Uploading

Lets see the important parts of the above HTML code :

  • enctype=”multipart/form-data”  : This attribute is mandatory for uploading in PHP, else this functionally will not work properly.
  • input type=”hidden” name=”MAX_FILE_SIZE” value=”51200″ : This will set the maximum size in bytes  of the files that you are willing to accept. As here we have set maximum length of 50 KB. You can allow more large uploads by changing settings in php.ini, upload_max_filesize attribute. After this you are ready to add the upload filed itself.
  • input type=”file” is an INPUT element with the type argument file.

PHP uploading Script :

 $file_dirctory = "d:/Freefeast/";
 foreach($_FILES as $file_name => $file_arr) 
 {
      echo "path: ".$file_arr["tmp_name"]."<br/>";
      echo "name: ".$file_arr["name"]."<br/>";
      echo "type: ".$file_arr["type"]."<br/>";
      echo "size: ".$file_arr["size"]."<br/>";
 
     if (is_uploaded_file($file_arr["tmp_name"])) 
     {
        move_uploaded_file($file_arr["tmp_name"], "$file_directory/".$file_arr["name"]) or die ("Error !! Could not copy");        echo "File Moved Successful<br/>";
     }
 }

In this script , $file_dir variable will store path information, and this path should be one that exist on your system. Information about the uploaded file become available to you in the $_FILES .

Through foreach we will go through every elements or attributes in $_FILES array.

$file_arr[“tmp_name“] :  Path to temporary file.  //$FILES[“fileupload”][“tmp_name”]

$file_arr[“name“] : Original name of uploaded file. //$FILES[“fileupload”][“name”]

$file_arr[“type“] : MIME type of uploaded file. //$FILES[“fileupload”][“type”]

$file_arr[“size“] : Size (in bytes) of uploaded file // //$FILES[“fileupload”][“size”]

is_uploaded_file () function accepts a path to an uploaded file and returns true only if the uploaded file is valid.

move_uploaded_file() function requires a path to the source and destination file and returns true if the move is successful and false if the file is not a valid file or file could not be found.

Output :

PHP File Upload

You can restrict type and size of the file to be uploaded.

// For Size :
If( $_FILES["file"]["size"] > 2000000)
{   
   die(“File Size too big!!!!”);
}

// For Different format :
If( $_FILES["file"]["type"] == “image/png” || $_FILES["file"]["type"] == “videp/avi”)
{
     die(“This format of file not allowed !!!”);
}

//You can apply like this also :
If( $_FILES["file"]["size"] > 2000000 ||  $_FILES["file"]["type"] == “image/png”)
{
    die("This format  & size not allowed");
}

Lets See Complete Code :

Example :

<html>
<head>
     <title>A simple file upload form</title>
</head>
<body>
    <form action="do_upload.php" enctype="multipart/form-data" method="POST">
        <input type="hidden" name="MAX_FILE_SIZE" value="51200"/>
        <p><strong>File to Upload:</strong> <input type="file" name="fileupload"/></p>
        <p><input type="submit" name="submit" value="upload!"></p>
    </form>
</body>
</html>

<?php

 if(isset($_POST["submit"]))
 {
      $file_directory = "d:/Freefeast/";
      foreach($_FILES as $file_name => $file_arr) 
      {
           echo "path: ".$file_arr["tmp_name"]."<br/>\n";
           echo "name: ".$file_arr["name"]."<br/>\n";
           echo "type: ".$file_arr["type"]."<br/>\n";
           echo "size: ".$file_arr["size"]."<br/>\n";
 
        if (is_uploaded_file($file_arr["tmp_name"])) 
        {
           move_uploaded_file($file_arr["tmp_name"], "$file_directory/".$file_arr["name"]) or die ("Couldn't copy");
           echo "file was moved!<br/>";
         }
      }
 }

?>