Warning: Trying to access array offset on value of type bool in /home/mukesh88/public_html/www.phpkida.com/wp-content/themes/blog-2020/templates/php.php on line 40

File Uploading is a common feature in today’s PHP applications. You will first have to understand the basics concept of 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 relocated to a target destination by a PHP script.

Configure The “php.ini” File

First, ensure that PHP is configured to allow file uploads or not.
In your “php.ini” file, search for the file_uploads directive and check file_uploads set On or not:

file_uploads = On

Just follow below steps for uploading a file.

  • Than clicks on the browse button and selects a file to upload from your local PC.
  • After select file full path to the selected file appears in the text filed then the user clicks the submit button.
  • The selected file is 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 that the file has arrived and then copies the file into an intended directory.
  • Finally PHP script show the success message.

Create The HTML Form

Create an HTML form that allow users to choose the file they want to upload:

Note: for upload any file form enctype attribute value should be enctype=”multipart/form-data” and make sure the form method should be post method=”post”.

<!DOCTYPE html>

<form action="uploadfile.php" method="post" enctype="multipart/form-data">
    Select file to upload:
    <input type="file" name="fileUpload" id="fileUpload">
    <input type="submit" value="Click Me Upload File" name="submit">


Create The Upload File PHP Script

The “uploadfile.php” file contains the code for uploading a file:

There we are using a global PHP variable called $_FILES. This variable is an associate double dimension array and keeps all the information related to uploaded file. So if the value assigned to the input’s name attribute in uploading form was file, then PHP would create following five variables −

  • $_FILES[‘file’][‘tmp_name’] − the uploaded file in the 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’] − the MIME type of the uploaded file.
  • $_FILES[‘file’][‘error’] − the error code associated with this file upload.

      $file_name = $_FILES>['fileUpload']['name'];
      $file_size = $_FILES['fileUpload']['size'];
      $file_tmp = $_FILES['fileUpload']['tmp_name'];
      $file_type = $_FILES['fileUpload']['type'];
      $file_ext = strtolower(end(explode('.',$_FILES['fileUpload']['name'])));
      move_uploaded_file($file_tmp, "uploads/".$file_name);
      echo "File has been uploaded!";

How check if file already exists or not

Here we are checking if the uploading file already exists in the “uploads” folder. If it is, than display the error message otherwise display success message.

    $file_name = $_FILES>['fileUpload']['name'];
    $file_tmp = $_FILES['fileUpload']['tmp_name'];
    // Check if file already exists
    if (file_exists($target_file))
       echo "Sorry, file already exists!";
        move_uploaded_file($file_tmp, "uploads/".$file_name);
        echo "Yes, File has been uploaded!";

Sign up for weekly update

Milkshake is almost ready. If you're interested in testing it out, then sign up below to get exclusive access.