Monday, September 28, 2020

How to Upload Files to Server in PHP



Hello, dear readers! welcome back to another section of my tutorial on PHP. In this section of our PHP tutorial, we will be studying about File Uploading in PHP.

A PHP script can be used with a Html form to allow users upload files to the web server. Initially files where uploaded to a temporary directory and then relocated to the target destination by a PHP script.

Information in phpinfo.php page describes the temporary directory that is used for the file uploads as upload_tmp_dir and the maximum permitted size of files that can be uploaded as upload_max_filesize. These parameters are set into PHP configuration file php.ini.


The process of uploading a file is as follows -

  • The user open the page that contains a Html form which features a text file, browse button and a submit button.
  • The user clicks the browse button and selects a file to upload from the local PC.
  • The full path of the selected file displays in the text field, then the user clicks on the submit button.
  • The selected file is sent to the temporary directory on the server.
  • The PHP script that was set out as the form handler in the form's action attributes checks if the file has arrived and then copies the file into an intended directory.
  • The PHP script confirms the success to the user.

As usual when writing files it is vital for both the temporary and final location of the files to have permissions set that enable file writing. If either of them is set to read-only, the process will fail.

An uploaded file could be a text file or an image file or any other document.


Creating an Upload Form

The following Html code below creates an uploader form. This form is having method attribute set to post and enctype attribute set to multipart/form-data.

<?php
   if(isset($_FILES['image'])){
      $errors= array();
      $file_name = $_FILES['image']['name'];
      $file_size =$_FILES['image']['size'];
      $file_tmp =$_FILES['image']['tmp_name'];
      $file_type=$_FILES['image']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
      
      $extensions= array("jpeg","jpg","png");
      
      if(in_array($file_ext,$extensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }
      
      if($file_size > 2097152){
         $errors[]='File size must be excately 2 MB';
      }
      
      if(empty($errors)==true){
         move_uploaded_file($file_tmp,"images/".$file_name);
         echo "Success";
      }else{
         print_r($errors);
      }
   }
?>
<html>
   <body>
      
      <form action="" method="POST" enctype="multipart/form-data">
         <input type="file" name="image" />
         <input type="submit"/>
      </form>
      
   </body>
</html>

Output

When the above code is executed, it will produce the following result -


RECOMMENDED: Strings in PHP

Creating an Upload Script

There's one global PHP variable callled $_FILES. This variable is an associate double dimension array that keeps all information that is related to uploaded files. So if the value assigned to the input's name attribute in upload form was file, then PHP would create following five attributes -

  • $_FILES['file']['tmp_name'] - This is the uploaded file in the temporary directory on the web server.
  • $_FILES['file]['name'] - This is the name of the uploaded file.
  • $_FILES['file']['size'] - This is the size in bytes of the file uploaded.
  • $_FILES['file']['type'] - This is the MIME type of uploaded file.
  • $_FILES['file']['error'] - Error code that is associated with the file upload.

Example

Following example should allow upload images and gives result as uploaded file information.

<?php
   if(isset($_FILES['image'])){
      $errors= array();
      $file_name = $_FILES['image']['name'];
      $file_size = $_FILES['image']['size'];
      $file_tmp = $_FILES['image']['tmp_name'];
      $file_type = $_FILES['image']['type'];
      $file_ext=strtolower(end(explode('.',$_FILES['image']['name'])));
      
      $extensions= array("jpeg","jpg","png");
      
      if(in_array($file_ext,$extensions)=== false){
         $errors[]="extension not allowed, please choose a JPEG or PNG file.";
      }
      
      if($file_size > 2097152) {
         $errors[]='File size must be excately 2 MB';
      }
      
      if(empty($errors)==true) {
         move_uploaded_file($file_tmp,"images/".$file_name);
         echo "Success";
      }else{
         print_r($errors);
      }
   }
?>
<html>
   <body>
      
      <form action = "" method = "POST" enctype = "multipart/form-data">
         <input type = "file" name = "image" />
         <input type = "submit"/>
			
         <ul>
            <li>Sent file: <?php echo $_FILES['image']['name'];  ?>
            <li>File size: <?php echo $_FILES['image']['size'];  ?>
            <li>File type: <?php echo $_FILES['image']['type'] ?>
         </ul>
			
      </form>
      
   </body>
</html>

Output

When the above code is executed, it will produce the following result -



Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial post, we will be discussing about the PHP Coding Standards.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.