We now have a youtube channel. Subscribe!

How to Upload Files to Server in PHP

File Uploading in PHP


Hello folks! welcome back to a new edition of our 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 to upload files to the server. Initially files are uploaded into a temporary directory and then later moved to a target destination by a PHP script.

Information in phpinfo.php page describes the temporary directory used for uploading of file 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 follows these steps below -

  • 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 chosen file shows in the text field then the user clicks on the submit button.
  • The chosen file is instantly sent to the temporary directory on the server.
  • The PHP script that was specified as the form handler in the form's action attribute checks if the file has arrived and copies the file into an intended directory.
  • The PHP script confirms the success to the user.

Usually when writing files it is necessary for both temporary and final locations to have permissions set that enable file writing. If either is set to be 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 -



Creating an Upload Script

There is one global variable in PHP known as $_FILES. This global PHP variable is an associate double dimension array that is used to keep all the information related to uploaded file. 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'] - Uploaded file in the temporary directory on the web server.
  • $_FILES['file]['name'] - Actual 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

Below example allows 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 our next tutorial post, we will be discussing about the PHP Coding Standards.

Feel free to ask your questions where necessary and we 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.

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.
© 2023 ‧ WebDesignTutorialz. All rights reserved. Developed by Jago Desain