All TalkersCode Topics

Follow TalkersCode On Social Media

Image Upload In PHP Code With Databases

Last Updated : Jul 1, 2023

Image Upload In PHP Code With Databases

In this tutorial we will show you the solution of image upload in PHP code with databases, a picture can be uploaded and displayed in a variety of ways on your PHP website.

The most easy way to do this is to upload the image to the server's directory and then change the image's name in the database.

This strategy is effective since the image will not take up any space in the database, allowing your web page to load more quickly.

Another option is to immediately insert the image into the database rather than uploading it to the server.

This strategy is not advised because the photographs consume a lot of storage space in the database, making it larger.

This will also cause your web pages to load more slowly. In this tutorial we will find out the best way to upload images in PHP code with databases.

Step By Step Guide On Image Upload In PHP Code With Databases :-

The combined programme that demonstrates how to do picture upload in PHP is shown below.

$msg = "";
if (isset($_POST['uploadfile'])) {
    $filename = $_FILES["choosefile"]["name"];
    $tempname = $_FILES["choosefile"]["tmp_name"];
        $folder = "image/".$filename;
    $db = mysqli_connect("localhost", "root", "", "Image_upload");
        $sql = "INSERT INTO image (filename) VALUES ('$filename')";
        mysqli_query($db, $sql);
        if (move_uploaded_file($tempname, $folder)) {
            $msg = "Image uploaded successfully";
            $msg = "Failed to upload image";
$result = mysqli_query($db, "SELECT * FROM image");
<!DOCTYPE html>
    <link rel="stylesheet" type="text/css" href="style.css" />
  <style type="text/css">
            width: 50%;
            margin: 20px auto;
            border: 2px solid #dad7d7;
            width: 50%;
            margin: 20px auto;
        form div{
            margin-top: 5px;
            float: left;
            margin: 5px;
            width: 280px;
            height: 120px;
            width: 70%;
            padding: 5px;
            margin: 15px auto;
            border: 1px solid #dad7d7;
            content: "";
            display: block;
            clear: both;
    <div id="wrapper">
         <form method="POST" action="" enctype="multipart/form-data">
     <input type="file" name="choosefile" value="" />
                <button type="submit" name="uploadfile">WAMP or XAMPP server
  1. The HTML programme above generates a simple HTML form with an option to upload a file from your computer and a "UPLOAD" button.
  2. It will use the POST method to upload the image file.
  3. The enctype parameter specifies the encoding format in which the data entered in the form must be encoded before it is sent to the server (enctype="multipart/form-data").
  4. This is a critical attribute; if it is not specified, the image will not be posted to the server.
  5. To begin, use the XAMPP/WAMP server to establish a database. Create a database called "Image Upload" and name it that. The next step is to make a new table in the database.
  6. You've made a new table called "Image." In the table, create two fields - int – id (11) and Filename – varchar(100)
  7. The mysqli connect() method will be used to connect to the database in the PHP software mentioned before.
  8. The data submitted in the HTML form using the POST method is saved in the variable $filename. You must use a SQL query to insert the data into the database.
  9. The mysqli query() method runs the SQL query and then saves the results into the database.

Conclusion :-

Instead of storing an image in base64 format in a MySQL database, I believe it is preferable to save it on the server and save the reference in the database table to maintain track of its position.

When compared to base64, it is faster and takes up less space in the database table. I hope this tutorial on image upload in PHP code with databases helps you.

Latest Tutorials