Current Location: Home> Latest Articles> PHP Image Upload and Display Function Detailed Explanation

PHP Image Upload and Display Function Detailed Explanation

gitbox 2025-07-18

PHP Image Upload and Display Function Detailed Explanation

In modern website development, image upload and display is a common and essential feature. This article will explain in detail how to implement image upload and display functionality using PHP, helping developers quickly master this skill and improve user experience.

Basic Steps for Uploading Images in PHP

The image upload process typically involves several steps: creating an HTML form, processing the uploaded file, validating the file, and saving it to the server. Below is a simple HTML upload form example:

In this form, users can choose an image to upload. Once submitted, the PHP script will process the file.

Receiving and Handling the Uploaded Image

In the upload.php file, we need to write code to receive the uploaded file and process it. Here is an example code for handling the file upload:

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["image"]["name"]);
    $uploadOk = 1;
    $imageFileType = strtolower(pathinfo($target_file, PATHINFO_EXTENSION));

    // Check if it's an image
    $check = getimagesize($_FILES["image"]["tmp_name"]);
    if($check !== false) {
        echo "The file is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "The file is not an image.";
        $uploadOk = 0;
    }

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

    // Only allow specific 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 upload is OK
    if ($uploadOk == 0) {
        echo "Sorry, your file was not uploaded.";
    } else {
        if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
            echo "The file " . htmlspecialchars(basename($_FILES["image"]["name"])) . " has been successfully uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
}

In this code, we first check if a file was uploaded, then validate its type and size. If everything is correct, the file is moved to the designated directory.

Displaying the Uploaded Image

Once the upload is successful, we need to display the uploaded image. We can read the file from a database or a specified directory and display it on the webpage. Below is an example code to display the uploaded images:

$dir = "uploads/";
$images = glob($dir . "*.{jpg,jpeg,png,gif}", GLOB_BRACE);
foreach($images as $image) {
    echo "<img src='" . $image . "' alt='Uploaded Image' />";
}

In this code, we use the glob() function to retrieve all image files from the specified directory and then display each one in a loop. Be sure to adjust the display style according to your needs.

Conclusion

This article demonstrated how to implement image upload and display functionality using PHP. Mastering these basic steps is crucial for improving the functionality and user experience of a website. In real-world projects, proper security measures and file validation are essential to ensure the smooth operation of the upload feature.

We hope this article has been helpful. If you have any questions or suggestions, feel free to leave a comment!