PHP File Upload

PHP

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

First we need to create HTML form for uploading files,

HTML Form

<html> 
  <body>
      <form action="fileupload.php" method="post" 
enctype="mutipart/form-data">
       <input type="file" name="file_upload" /> 
      <input type="submit" value="UPLOAD" />
     </form>
  </body> 
</html>

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:

<?php
   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";
          else
               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.

$ext=strrchar($file_name,".");
$extarr=array(".txt",".pdf",".doc");
if(in_array($ext,$extarr)) {
do{
echo $orgname=md5(rand(1,100)).ext;
}while(file_exists($path).$orgname);

fileupload.php

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

<?php
if(isset($_POST["submit"])){
    $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 == 
"application/txt"){
       // only allow pdf and text files
       if($file_size <="25000"){
          // checking size of uploaded files
          $ext=strrchar($file_name,".");
          $extarr=array(".txt",".pdf");
          if(in_array($ext,$extarr)) {
             //random name
             do{
                $orgname = md5(rand(1,100)).ext;
              }while(file_exists($path).$orgname);
              if(move_uploaded_file($file_tmpname,$path.$orgname))
                 echo "File uploaded";
              else 
                echo"Something went wrong";
           } 
       }
}
?>

 

Leave a Reply

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