IDIOT DEVELOPER

Basic File Upload in PHP
Basic File Upload in PHP
DOWNLOAD

For every developer there come a time when s/he is required to add the file upload functionality in his program, that allows user to upload files on the server. Since allowing user customization has become more and more important on websites these days, I thought I should share how easy it is to handle file uploads in PHP. So if you don’t know how to upload files in PHP then don’t worry, this tutorial is for you.

 

In this tutorial I am going to develop a PHP Image file uploader. It only accepts and uploads the image files only.

 

Before begining, make sure that PHP is configured to allow file uploads.
In php.ini find file_uploads directive, and set it On

 


file_uploads = On

 

Now we will design a simple UI for the file upload form.

 

HTML Code

 


<div class="upload-form">
 <h2>BASIC FILE UPLOAD IN PHP</h2>
 <form method="post" action="upload.php" enctype="multipart/form-data">
  <label for="photo">Select photo: </label>
  <input type="file" name="photo" id="photo">
  <br/>
  <button type="submit">Upload</button>
 </form>
</div>

 

You’ll need to use the multipart/form-data value for the form’s enctype property.

 

CSS Code

 


body{
 font-family: sans-serif;
 }
 .upload-form{
 background: #f2f2f2;
 width: 400px;
 padding: 10px;
 border-radius: 5px;
 border: 1px solid #ccc;
 margin-left: auto;
 margin-right: auto;
 }
 input[type="file"]{
 border: 1px solid #ccc;
 border-radius: 5px;
 }
 button[type="submit"]{
 background: #333;
 border: 1px solid #333;
 padding: 10px 30px;
 margin-top: 10px;
 color: #fff;
 border-radius: 5px;
 }
 button[type="submit"]:hover{
 cursor: pointer;
 }

 

Now we are going to learn about the core part of the php file upload.

 

While uploading a file we need to consider many things in mind. What kind of file we need to upload, like we can upload an image file, a text file, a pdf file or any other file.

 

Basic File Information

First of all we will make sure that the file is selected to upload for that we will use $_FILES[‘photo’], here photo is the name of the file upload input tag.

 


<input type="file" name="photo" id="photo">

 

Here we check that a file has been uploaded or not.

 


if(isset($_FILES['photo']) && $_SERVER['REQUEST_METHOD'] == "POST"){
  ...
}

 

The $_FILES[‘photo’] is an array with the file information, you can have a look at it with print_r($_FILES[‘photo’]).

 


$file_name = $_FILES['photo']['name'];
$file_type = $_FILES['photo']['type'];
$file_tmp = $_FILES['photo']['tmp_name'];
$file_error = $_FILES['photo']['error'];
$file_size = $_FILES['photo']['size'];

These above are details of the file selected to upload. After that we will check for any error in the file. The $file_error tells about the error. if its value equals to zero(0) then no error is there, otherwise an error is there.

 


if($file_error <= 0){
  ...
}else{
  echo 'Error in file.';
  return false;
}

 

Check the Extension

The type of file is generally determined by the extension of the file. As we wants to upload an image file, so we will only accepts file extension jpeg, jpg, png, bmp, gif. To extract the extension we are going to use the explode() and end() function. There won’t be any problem even if the file name has a dot in it.

 


//Extension allowed.
$ext_allows = array("jpeg", "jpg", "png", "bmp");

$file_ext = explode(".", $file_name);
$file_ext = end($file_ext);
$file_ext = strtolower($file_ext);

 

Now we have the file extension in the $file_ext variable, we can compare the file extension with the extension allowed.

 


if(in_array($file_ext, $ext_allows) === true){
  //Correct extension.
  ...
}else{
  //Incorrect extension.
  echo 'Extension not allowed.';
  return false;
}

 

 

Check the Size

After checking the extension, we now checks the size of the file. Like some website have a limit for the size of file to be uploaded. We are also going to implement the same functionality.

 


$size_allow = 2097152; //2MB.

//Checking the size of the file.
if($file_size <= $size_allow){
  ...
}else{
  //Inappropriate size.
  echo 'File size must be 2MB or less in size.';
  return false;
}

 

Upload File

Now the file has passed all the validation test. Now we are ready to upload it. We are going to give the file a new(unique) name to make sure that no other file have that name. We are going to upload the file into the uploads folder, and then we are going to display the image.

 


$new_name = time() . '.' . $file_ext;
move_uploaded_file($file_tmp, 'uploads/'.$new_name);
echo 'File uploaded successfully.';

echo '<br/><img src="uploads/'.$new_name.'" style="height: 200px; width: 200px;">';

 

Now finally e have uploaded the file into the uploads folder and then display it on the browser.

 

Complete Code

 


<?php

if(isset($_FILES['photo']) && $_SERVER['REQUEST_METHOD'] == "POST"){
/*
echo '<pre>';
print_r($_FILES['photo']);
echo '</pre>';
*/

$file_name = $_FILES['photo']['name'];
$file_type = $_FILES['photo']['type'];
$file_tmp = $_FILES['photo']['tmp_name'];
$file_error = $_FILES['photo']['error'];
$file_size = $_FILES['photo']['size'];

//Extensions allowed.
$ext_allows = array("jpeg", "jpg", "png", "bmp");
$size_allow = 2097152; //2MB.

//Extracting the extension from the file.
$file_ext = explode(".", $file_name);
$file_ext = end($file_ext);
$file_ext = strtolower($file_ext);

//Checking for error in the file.
if($file_error <= 0){
  //No Error in file.

  //Checking for correct extension of the file.
  if(in_array($file_ext, $ext_allows) === true){
    //Correct extension.

    //Checking the size of the file.
    if($file_size <= $size_allow){
      //Appropriate size.

      //Uploading the file.
      $new_name = time() . '.' . $file_ext;
      move_uploaded_file($file_tmp, 'uploads/'.$new_name);
      echo 'File uploaded successfully.';

      echo '<br/><img src="uploads/'.$new_name.'" style="height: 200px; width: 200px;">';

    }else{
      //Inappropriate size.
      echo 'File size must be 2MB or less in size.';
      return false;
    }
  }else{
    //Incorrect extension.
    echo 'Extension not allowed.';
    return false;
  }

}else{
  //Error in the file.
  echo 'Error in the file.';
  return false;
}

}else{
echo 'Failed';
}

?>

 

You can download the complete code from my github acccount.

Leave a Reply

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