JavaScript Tutorial

File Uploading

You can easily upload files to the server with the help of PHP. Both binary and text files can be uploaded to the server. To ensure the configuration of file uploads, check the following setting in your php.ini file.

file_uploads = On

PHP Global $_FILES contains all necessary information about the file being uploaded to the server. For example :  file_name, file_type, file_size etc. Suppose the name of the file to be uploaded is ‘file1’.

$_FILES[‘file1’][‘name’]

contains name of the file

$_FILES[‘file1’][‘tmp_name’]

contains temporary name of the file that was stored on server 

$_FILES[‘file1’][‘type’]

contains MIME type of the file

$_FILES[‘file1’][‘size’]

contains the size of the file

$_FILES[‘file1’][‘error’]

contains error code associated with the file

move_uploaded_file() method

It is used to move the uploaded file to a specified location on the server. File must be uploaded through POST request method.

//syntax
move_uploaded_file($filename , $destination);

An Example of File Upload: 

STEP #1 Create an HTML file form.html 

Let's create a form in form.html to upload a file. After form submit, the file will be uploaded to the server using php file upload script. Form must fulfill the following requirements: 

  • Form must be using POST method for data submission.
  • enctype="multipart/form-data" must be mentioned as an attribute of <form> tag.
  • <input> tag with type=”file” will be used for the selection of file through Browse button.
<!--HTML Form-->
<!DOCTYPE html>
<html>
<body>
     <form action="fileUpload.php" enctype="multipart/form-data" method="post">
           Select a File :
           <input type="file" name="file" id="filename">
           <input type="submit" name="submit" value="Upload" >
    </form>
</body>
</html>

STEP #2 Now create  fileUpload.php to write php script

Following php script is to upload a file on server.

<?php
      $flag = 1;
      $targetDirectory = "images/";
      $targetFile = $targetDirectory . basename($_FILES["file"]["name"]);

      if(isset($_POST["submit"]) && $_POST["submit"] == “Upload”) {
            $checkImage = getimagesize($_FILES["file"]["tmp_name"]);
            if($checkImage !== false) {
                  $flag = 1;
            } else{
                  $flag = 0;
            }
      }

      if($flag == 1){
            if (move_uploaded_file($_FILES["file"]["tmp_name"], $targetFile)) {
                  echo "File Uploading Successful";
            } else{
                  echo "File Uploading Failed";
            }
      }
?>

 

Here are few additional checks ✅ while uploading file:

<!-- script to check FILE-SIZE-->
<?php
     if ($_FILES["file"]["size"] > 10000) {
          echo "File size is smaller than expected";
     $flag = 0;
     }
?>
<!-- script to check FILE-TYPE-->
<?php
     $fileType = strtolower(pathinfo($targetFile, PATHINFO_EXTENSION));
     if($fileType != "jpeg" && $fileType != "jpg" && $fileType != "png") {
          echo "Only JPEG, JPG and PNG files are allowed.";
          $flag = 0;
     }
?>
Go back to Previous Course