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.




    
    Images Upload Form - www.pakainfo.com


    

Upload Images

Note: Only .jpg, .jpeg, .gif, .png formats allowed to a max size of 5 MB.

Step 2: Processing the uploaded Images

 "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.
 0){
    echo "Error: " . $_FILES["profile"]["error"] . "
"; } else{ echo "File Name: " . $_FILES["profile"]["name"] . "
"; echo "File Type: " . $_FILES["profile"]["type"] . "
"; echo "File Size: " . ($_FILES["profile"]["size"] / 1024) . " KB
"; 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.

Leave a Comment