Uploading File Using PHP

To upload a file on the server using PHP, we use a simple form for user input and then use PHP to access the uploaded file and process it.

Some Terminologies

Input Method

The input element used for uploading file is

<input type="file" name="file-name" />

Enctype Attribute

We need to add the attribute enctype="multipart/form-data" inside the <form> tag. This is required to encode the form data that is being submitted. The enctype attribute can be used only with POST method.

$_FILES Variable

The variable used to access the file uploaded is $_FILES['filename']['type of attribute']. There is no POST variable required to access the uploaded files.

$_FILES – Types of Attributes

$_FILES[‘file-name’][‘name’]

This attribute provides us the original name of the file before it was uploaded.

$_FILES[‘file-name’][‘tmp_name’]

  • When a file is uploaded, it is first uploaded to a temporary location in the server from where it is moved to the target directory. This attribute is used to get the temporary location of an uploaded file.
  • We move the file from temporary location to target directory by using move_uploaded_file($tmp, $target_file) where $tmp is the temporary location of the file and $target_file is the final location of the file (target directory + file name).
  • The target directory should have the permissions 755 atleast so that any user can upload a file in that folder. We use Files folder to upload the files.

$_FILES[‘file-name’][‘size’]

This attribute is used to get the size of the file in bytes.

$_FILES[‘file-name’][‘type’]

This provides the type(MIME type) of the file, for instance, jpg file or pdf file. However, this should not be used as it provides mime type provided by the browser and therefore is not safe to use. Therefore, for getting the extension of the file, we use pathinfo($file_name, PATHINFO_EXTENSION) where $filename is the name of the file.

In the code below, the user submits a form where he is required to fill in his name and an image file with jpg, jpeg and png extensions and maximum size of 100 Kb. When the user submits the file, the PHP script checks whether all the fields are filled or not and whether the file type and size are correct or not. If everything is found to be correct, then the image is uploaded in the Files folder which is at the same location as index.php otherwise error message is displayed.

Index.php HTML Script

<form action="" method="post" enctype="multipart/form-data">
    <label>Name</label> <br>
    <input name="name" type="text" size="30" required /><br><br>
    <label>Upload image (jpg, jpeg and png extensions only)</label> 
    <input name="img" type="file" size="30" required /><br><span>(Max file size : 100Kb)</span><br><br>
    <input name="submit" type="submit" id="submit" class="submit" value="Submit" />
</form>

Index.php PHP script

<?php

    if(isset($_POST['name']) && isset($_FILES['img']['name'])){

        $name = $_POST['name'];
        $file_name = $_FILES['img']['name'];    //Getting the name of the original file
        if($name=="" || $file_name==""){
            echo "<p style='color:red;font-size:15px;'>Fill in all the fields</p>";
        }
        else{
            //Checking file type by first getting the extension
            $ext = pathinfo($file_name, PATHINFO_EXTENSION);
            if($ext!="jpg" && $ext!="png" && $ext!="jpeg")
                echo "<p style='color:red;font-size:15px;'>Upoad image with jpg,jpeg or png extensions only</p>";
            else{

                if($_FILES["img"]["size"]>102400)   //Checking the size of the file
                    echo "<p style='color:red;font-size:15px;'>Maximum file size allowed is 100Kb.</p>";
                else{
                    $tmp = $_FILES['img']['tmp_name'];  //Getting the temporary location of the file
                    $target_dir = "your-target-directory-location/files/";
                    $target_file = $target_dir.basename($file_name);

                    if (move_uploaded_file($tmp, $target_file)) //Moving the file to target directory
                    {
                        echo "<p style='color:green;font-size:15px;'>Hello ".$name.". The file ". $file_name. " has been uploaded.</p>";
                        echo '<p style="font-size:15px;"><a href="files/'.$file_name.'" target="_blank">Click here</a> to view the uploaded image.</p>';
                    } 
                    else {
                        echo "<p style='color:red;font-size:15px;'>Sorry, there was an error uploading your file.</p>";
                    }
                }

            }
        }

    }
?>

Uploading a file

Uploading File

Incorrect File Type

Incorrect file type

File with size greater than maximum size allowed
Greater size

File with all correct credentials

Correct File

Please feel free to download the source code for reference.

View Demo Download source

This content has been helpful to you?

Thanks for contributing!

Yes No