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.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

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.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Images Upload Form - www.pakainfo.com</title>
</head>
<body>
    <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>
    </form>
</body>
</html>

Step 2: Processing the uploaded Images

<?php

if($_SERVER["REQUEST_METHOD"] == "POST"){

    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.

<?php
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.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Get all checked Checkbox Values in AngularJS
  2.   PHP wordpress get timezone Example - MySQL
  3.   javascript Form validation Source code
  4.   PHP Functions - MY SQL Connects and Global Variable
  5.   VueJS String substring() Method
  6.   Sending Bulk Emails in PHP with PHPMailer
  7.   10 Useful PHP Filesystem Functions with Example
  8.   Advanced jquery Expert Tips for Developers
  9.   AJAX Pagination with Laravel Example From Scratch
  10.   Very Simple PHP Script to Upload Image or file from an URL

Leave a Reply

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