PHP File Uploading

PHP provides the ability to upload files to your server. You can upload one or more files through PHP. With PHP, you can upload not only text files but also binary file like image etc.

PHP provides complete control over the file you upload. PHP provides authentication and file manipulation functions for this. With PHP, you can control that once you upload a file you want to do with that file.

Configure The “php.ini” File

To upload files to PHP, first of all you should find out whether file upload is allowed in php.ini or not. This file controls all PHP operations. The file _uploads variable should be value on set in this file. If its value is not on, then you need to change it.

file_uploads = On

Create The HTML Form

You need an HTML form to upload a file through PHP. By this form the user will select a file from his computer.

After selecting File, users will submit the form at the same time your PHP file will execute. This PHP file will contain the code that will upload the file to the directory define successfully on the server.

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

In the above example, action, method and enctype attributes have been defined in the form tag respectively. In this, with the attribute attribute, you will define the php file which will be executed if the form is submitted. If you are writing PHP code and HTML code in the same file then you can leave this attribute blank.

The second attribute method is to use the data to define the method to be sent. In the case of file uploading, this attribute’s value defines you only post.

The third attribute is enctype. It define the attoding file’s encoding type. File attribution has to be defined for this attribute.

The way you input text to define text box is to define. Similarly, input type file is defined for file. As has been done in the above example.

Create The Upload File PHP Script

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>

Note: You will need to create a new directory called “uploads” in the directory where “upload.php” file resides. The uploaded files will be saved there.

Check if File Already Exists

// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}

Limit File Size

// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}

Limit File Type

// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}