Contact me See Resume

Upload file using PHP - nareshonline.com

Upload file using PHP

Catagory : PHP and mySQLi Posted : September, 07, 2018
upload file using PHP

This is a simple tutorial to learn how to upload file using PHP with the help of HTML form. PHP makes it very easy, which is required in most of websites.

Upload file by user can be harmful to your website, because hackers can effect the data always upload file with some security and precautions like file size, file type, etc.

Steps to upload file using PHP

First we need a HTML form to upload file.

<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" enctype="multipart/form-data">
  <p><input type="file" name="myfile"></p>
        <p><input type="submit" name="submit" value="Upload files"></p>
</form>

Important

We must have a attribute enctype = “multiplat/form-data” in form tag. This attribute mention how data should be encoded before uploading to the server.

type =”file” will create an array by using PHP Global function $_FILE[] from which is going to upload and all information in separate elements like file name, file size, temporary location.

PHP Script

When user click submit button to upload file then php script comes in action.

if(isset($_POST["submit"])){

}

Getting file name

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

Getting temporary location of file from file array. Wen user upload files php hold the file in temporary location on server during the upload process.

$file_tmp = $_FILES['myfile']['tmp_name'];

Now we will get the file size from file array. It will help in providing restriction  file size.

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

Getting file extension from the file using explode function. This function convert the  string to array with specified separator.

$get_extension = explode('.',$file_name);

Now get proper extension of file using end function and also convert the extension in lower.

$get_extension = strtolower(end($get_extension));

here I am using uniqid() function to prevent the upload file with same name.

$new_file = uniqid().'.'.$get_extension;

Set the location where you want to upload the file. Here I am using uploads directory to upload file.

$file_store = "uploads/".$new_file;

Now make the required validation before upload file on destination folder, like file size, file type with if condition

Here I am allowing only jpg, png and gif image with less than or equal to 200kb.

if ($get_extension=='jpg' || $get_extension=='png' || $get_extension=='gif') {
    if($file_size>=200000){
      echo "<script>alert('Max. file size should be 200 KB.')</script>";
    }
    else{
      if(move_uploaded_file($file_tmp, $file_store)){
        echo "<script>alert('$file_name is uploaded successfully with uniq ID')</script>";

      };
    }
  }	
  else{
    echo "<script>alert('Please select only jpg, png or gif image only.')</script>";
  }
}

Finally  as if no errors occurred the move_upload function will upload file to destination folder from temporary location with unique name.

Complete Code for Upload file using PHP

<?php 
if(isset($_POST["submit"])){
  $file_name = $_FILES['myfile']['name'];

  $file_tmp = $_FILES['myfile']['tmp_name'];
  
  $file_size = $_FILES['myfile']['size'];

  $get_extension = explode('.',$file_name);
  
  $get_extension = strtolower(end($get_extension));	
  
  $new_file = uniqid().'.'.$get_extension;

  $file_store = "uploads/".$new_file;

  //move_upload_file() function is use to store the file to destination folder from the temperary folder. It have two parameter 1. for temperary file 2. destination folder.
        if ($get_extension=='jpg' || $get_extension=='png' || $get_extension=='gif') {
    if($file_size>=200000){
      echo "<script>alert('Max. file size should be 200 KB.')</script>";
    }
    else{
      if(move_uploaded_file($file_tmp, $file_store)){
        echo "<script>alert('$file_name is uploaded successfully with uniq ID')</script>";
      }
    }
  }	
  else{
    echo "<script>alert('Please select only jpg, png or gif image only.')</script>";
  }
}


?>

 

Leave a Reply

You have to agree to the comment policy.