Home » How to Upload Images on Server in PHP?

How to Upload Images on Server in PHP?

In this upload image on server php Article i will learn how to upload Images on remote server using a Basic HTML form and PHP code.

Uploading Images with PHP server Side

You can upload server side using PHP any kind of file like images Like jpg, PNG, JPEG, GIF, videos like as a MP4, ZIP Images, any available Microsoft Office documents like as a CSV, PPT, PDFs, as well as executables Images as well as a wide range of other more file types.

Related topic :

Here are some more upload image on server php related to this Tutorials:

Step 1: Creating an HTML form to upload the Image

The following step by step helpful example will make a simple HTML form that can be used to upload Images.

Read Also:  10 Useful PHP Filesystem Functions with Example

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <title>Images Upload Form - www.pakainfo.com</title>
    <form action="upload-manager.php" method="post" enctype="multipart/form-data">
        <h2>Upload Images</h2>
        <label for="fileSelect">Images name:</label>
        <input type="file" name="profile" id="fileSelect">
        <input type="submit" name="submit" value="Upload">
        <p><strong>Note:</strong> Only .jpg, .jpeg, .gif, .png formats allowed to a max size of 5 MB.</p>

Step 2: Processing the uploaded Images



    if(isset($_FILES["profile"]) && $_FILES["profile"]["error"] == 0){
        $allowed = array("jpg" => "image/jpg", "jpeg" => "image/jpeg", "gif" => "image/gif", "png" => "image/png");
        $filename = $_FILES["profile"]["name"];
        $filetype = $_FILES["profile"]["type"];
        $filesize = $_FILES["profile"]["size"];

        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        if(!array_key_exists($ext, $allowed)) die("Error: Please select a valid Image format.");

        $maxsize = 5 * 1024 * 1024;
        if($filesize > $maxsize) die("Error: Image size is larger than the allowed limit.");

        if(in_array($filetype, $allowed)){
            // Check whether file exists before uploading it
            if(file_exists("upload/" . $filename)){
                echo $filename . " is already exists.";
            } else{
                move_uploaded_file($_FILES["profile"]["tmp_name"], "upload/" . $filename);
                echo "Your file was uploaded successfully.";
        } else{
            echo "Error: There was a problem uploading your Image. Please try again."; 
    } else{
        echo "Error: " . $_FILES["profile"]["error"];

Explanation of PHP Source Code

  • $_FILES["profile"]["name"] — This php array data value identify the original name of the Image, adding the Image extension. It doesn’t include the file path.
  • $_FILES["profile"]["type"] — This php array data value identify the MIME type of the Image.
  • $_FILES["profile"]["size"] — This php array data value identify the Image size, in bytes.
  • $_FILES["profile"]["tmp_name"] — This php array data value identify the temporary name adding full path that is assigned to the file once it has been uploaded to the server.
  • $_FILES["profile"]["error"] — This php array data value identify error or status code associated with the Image upload, e.g. it will be 0, if there is no error.
Read Also:  Validate is not a function in laravel

if($_FILES["profile"]["error"] > 0){
    echo "Error: " . $_FILES["profile"]["error"] . "<br>";
} else{
    echo "File Name: " . $_FILES["profile"]["name"] . "<br>";
    echo "File Type: " . $_FILES["profile"]["type"] . "<br>";
    echo "File Size: " . ($_FILES["profile"]["size"] / 1024) . " KB<br>";
    echo "Stored in: " . $_FILES["profile"]["tmp_name"];

I hope you get an idea about image upload in php code with databases.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Read Also:  Laravel Collection Contains Example

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  how to store json data in mysql using php?
  2. Read Also:  Vue.js GET ajax request Fetch Data using PHP
  3. Read Also:  Animation (ngAnimate) using Angular Example
  4. Read Also:  Laravel Merge complex Eloquent queries
  5. Read Also:  How to Quickly Index New Post In Google Search
  6. Read Also:  Very Simple PHP Script to Upload Image or file from an URL
  7. Read Also:  Great UNIX Commands Tutorial for Beginners
  8. Read Also:  Angular Dynamic Creating Factory Components
  9. Read Also:  Laravel 6 Switch Case Statements
  10. Read Also:  Laravel 6 Switch Case Statements

Leave a Comment

Your email address will not be published. Required fields are marked *