How to Upload Image in PHP and Store in Folder

2 years ago admin PHP

In this tutorial we are going to see how to upload and store files in a folder using PHP, also we will see how to check file size, file extension and if the file already exists in the folder.


Create the form

Let's first create the form for uploading the file, the form contains only one field that allows users to choose the file to upload.

                                                    
                                                                                                                
<!DOCTYPE html>
<html>
    <body>
        <form method="post" enctype="multipart/form-data">
            Select image to upload:
            <input type="file" name="file" id="file">
            <input type="submit" value="Upload Image" name="submit">
        </form>
    </body>
</html>

PHP script for uploading and storing the file

Next, we add the PHP code to store the file in the folder.

                                                        
                                                                                                                        
<?php
    // Check for form submission
    if(isset($_POST["submit"])) {
        $directory = "uploads/";
        $file = $directory . basename($_FILES["file"]["name"]);
        $uploaded = true;
        $file_ext = strtolower(pathinfo($file,PATHINFO_EXTENSION));

        // Check if file already exists
        if (file_exists($file)) {
            echo "Sorry, file already exists.";
            $uploaded = false;
        }

        // Check file size
        if ($_FILES["file"]["size"] > 1000000) {
            echo "Sorry, your file is too large.";
            $uploaded = false;
        }

        // Check file extension
        if($file_ext != "jpg" && $file_ext != "png" && $file_ext != "jpeg"
            && $file_ext != "gif" ) {
            echo "Only JPG, JPEG, PNG & GIF files are allowed.";
            $uploaded = false;
        }

        // Check if file is uploaded
        if (!$uploaded) {
            echo "File was not uploaded.";
        } else {
            if (move_uploaded_file($_FILES["file"]["tmp_name"], $file)) {
                echo "File ". htmlspecialchars( basename( $_FILES["file"]["name"])). " uploaded successfully.";
            } else {
                echo "Error while uploading your file.";
            }
        }
    }
?>

Related Tuorials

How to Use Variables and Data Types in PHP (A Clear Guide for Absolute Beginners) Part 2

In the second part of this guide, we will see more PHP types.Also, we will see how to check a variab...


How to Use Variables and Data Types in PHP (A Clear Guide for Absolute Beginners) Part 1

The first thing you need to understand when learning PHP is variables and data types. In this g...


How to Install and Run PHP on Your Localhost (Step-by-Step Guide)

If you want to learn PHP or build a web application with it, the first step is to create a working l...


CRUD Application with PHP PDO Ajax, and MySQL Part 2

In the second part of this tutorial, we will get all the students from the database and display them...


CRUD Application with PHP PDO Ajax, and MySQL Part 1

In this tutorial we will see how to create a crud application with PHP PDO Ajax, and MySQL, the user...


How to Sort Associative Arrays in Descending Order According to the Key Value in PHP

in this lesson, we will see how to sort associative arrays in descending order according to the key...


How to Sort Associative Arrays in Ascending Order According to the Key Value in PHP

in this lesson, we will see how to sort associative arrays in ascending order according to the key v...


How to Sort Associative Arrays in Descending Order According to the Value in PHP

in this lesson, we will see how to sort associative arrays in descending order according to the valu...


How to Sort Associative Arrays in Ascending Order According to the Value in PHP

in this lesson, we will see how to sort associative arrays in ascending order according to the value...


How Do you Sort an Array in Descending Order in PHP

In this lesson, we will see how to sort descending an array in PHP, we will use the rsort() function...