Upload File in PHP

Admin   PHP   209  2020-12-23 21:16:36

UPLOAD FILE IN PHP A PHP script can be used with an HTML form allowing users to upload files to the server. These files are first uploaded to a temporary directory, then moved to a destination by a PHP script.

Upload file in php

Upload file in php

The information in the phpinfo.php page describes the temporary directory used for files uploaded as upload_tmp_dir and the limited size of the files that can be uploaded starting with upload_max_filesize. These parameters are set in the php.ini configuration file.

To upload a file, follow these steps:

  • The user opens a page containing an HTML form that is a text file, a browse button, and a submit button.
  • The user presses the browse button and selects a file to upload from the local machine.
  • The full path to the selected file will appear in the text field, after which the user clicks the Submit button.
  • The selected file is sent to a temporary directory on the server.
  • The PHP script, which is specified as a Form Handler in the action property of the form, checks whether the file has arrived and then copies the file to the desired directory.
  • PHP script validates successfully to the user.

Usually, when writing files, both the temporary directory and the destination directory must have permissions set to write-enabled. If either is set to read-only, then the process will fail.

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

Create upload form in PHP

Below is the HTML code that creates an upload form. This form has the method property set to post and the enctype property 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'])));

      

      $expensions = array("jpeg", "jpg", "png");

      

      if (in_array($ file_ext, $expensions) === false) {

         $errors[] = "This image format was not accepted, please choose JPEG or PNG.";

      }

      

      if ($file_size> 2097152) {

         $errors[] = 'The file size should be 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>

Save the above program in a file called test.php in htdocs , then open a browser and type http://localhost: 8080/test.php will produce the following result:

Create script upload in PHP

There is a global PHP variable called $_FILES . This variable is an associative array and holds all information regarding the uploaded file. So, if the value assigned to the name property of the input in the upload form is file , then PHP can create the following 5 variables:

  • $_FILES ['file']['tmp_name'] - The file has been uploaded to a temporary directory on the Web Server.
  • $_FILES ['file']['name'] - The actual name of the uploaded file.
  • $_FILES ['file']['size'] - The size in bytes of the uploaded file.
  • $_FILES ['file']['type'] - MIME type of the uploaded file.
  • $_FILES ['file']['error'] - Encrypted error related to this upload file.

 

For example

Below is an example that allows you to upload images and return the results 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'])));

      

      $expensions = array("jpeg", "jpg", "png");

      

      if (in_array($ file_ext, $expensions) === false) {

         $errors[] = "This image format was not accepted, please choose JPEG or PNG.";

      }

      

      if ($file_size > 2097152) {

         $errors[] = 'The file size should be 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> Submit a file with the name: <?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>