How to Upload a File in PHP

09/05/2021

Contents

In this article, you will learn how to upload a file in PHP.

PHP File Upload

Here’s a simple PHP code snippet to upload a file:

<?php
  if(isset($_POST['submit'])){
    $file = $_FILES['file'];
  
    $fileName = $_FILES['file']['name'];
    $fileTmpName = $_FILES['file']['tmp_name'];
    $fileSize = $_FILES['file']['size'];
    $fileError = $_FILES['file']['error'];
    $fileType = $_FILES['file']['type'];
  
    $fileExt = explode('.', $fileName);
    $fileActualExt = strtolower(end($fileExt));
  
    $allowed = array('jpg', 'jpeg', 'png', 'pdf');
  
    if(in_array($fileActualExt, $allowed)){
      if($fileError === 0){
        if($fileSize < 1000000){
          $fileNameNew = uniqid('', true).".".$fileActualExt;
          $fileDestination = 'uploads/'.$fileNameNew;
          move_uploaded_file($fileTmpName, $fileDestination);
          header("Location: index.php?uploadsuccess");
        } else {
        echo "Your file is too big!";
        }
      } else {
        echo "There was an error uploading your file!";
      }
    } else {
      echo "You cannot upload files of this type!";
    }
  }
?>

This code assumes that you have an HTML form that allows file selection and submission. The form should have a file input with the name attribute set to "file" and a submit button with the name attribute set to "submit". When the form is submitted, this PHP code checks if the file is of an allowed type (e.g., jpg, jpeg, png, pdf), then checks if there was an error during the upload process and if the file size is within an acceptable limit. If all checks pass, the file is moved to the "uploads" directory and the user is redirected to a page that indicates a successful upload.

Here's a more in-depth explanation of the code:

  • if(isset($_POST['submit'])){: This line checks if the form has been submitted. The $_POST variable is an associative array that contains the data submitted from a form using the POST method. In this case, we are checking if the submit button has been clicked by checking if the value of $_POST['submit'] exists.
  • $file = $_FILES['file'];: This line creates a variable $file that is equal to the $_FILES associative array. The $_FILES array contains information about the file that was uploaded, such as the name, type, size, and temporary location.
  • $fileName = $_FILES['file']['name'];: This line creates a variable $fileName that is equal to the original name of the file.
  • $fileTmpName = $_FILES['file']['tmp_name'];: This line creates a variable $fileTmpName that is equal to the temporary location of the file on the server.
  • $fileSize = $_FILES['file']['size'];: This line creates a variable $fileSize that is equal to the size of the file in bytes.
  • $fileError = $_FILES['file']['error'];: This line creates a variable $fileError that is equal to an error code, if any, that occurred during the file upload process.
  • $fileType = $_FILES['file']['type'];: This line creates a variable $fileType that is equal to the MIME type of the file.
  • $fileExt = explode('.', $fileName);: This line creates a variable $fileExt that is equal to an array that contains the parts of the file name, separated by a period.
  • $fileActualExt = strtolower(end($fileExt));: This line creates a variable $fileActualExt that is equal to the lowercase extension of the file. The end function returns the last element of an array, and the strtolower function converts a string to lowercase.
  • $allowed = array('jpg', 'jpeg', 'png', 'pdf');: This line creates an array $allowed that contains a list of file extensions that are allowed to be uploaded.
  • if(in_array($fileActualExt, $allowed)){: This line checks if the actual file extension is in the list of allowed extensions. If it is, the code inside the curly braces will run.
  • if($fileError === 0){: This line checks if there were any errors during the file upload process. If there were no errors, the code inside the curly braces will run.
  • if($fileSize < 1000000){: This line checks if the size of the file is less than 1 MB (1000000 bytes). If it is, the code inside the curly braces will run.
  • $fileNameNew = uniqid('', true).".".$fileActualExt;: This line creates a new, unique name for the file. The uniqid function generates a unique identifier