Friday, December 8, 2023
Hire us on Freelancer.com for web development service
HomeProjectsPhpHow To Upload An Image in database With PHP

How To Upload An Image in database With PHP

The ability to upload and save images in a database is an important ability for web developers in today’s digital world. Whether you’re creating an e-commerce site, a social networking platform, or a content management system, learning how to upload images using PHP will significantly enhance the usefulness of your online application. We will walk you through the process of adding images to a database step by step in this article we will walk you through the process of How To Upload An Image in database With PHP step by step.

Preparing Your Environment

Before you can start uploading images to a database, you need to ensure that your development environment is set up correctly. Here are the key components you’ll need:

PHP: Make sure PHP is installed on your server or local development environment.

Web Server: Apache, Nginx, or any other web server of your choice should be configured and running.

Database: Set up a database where you’ll store the images. MySQL and PostgreSQL are popular choices.

HTML Form: Create an HTML form that allows users to select and submit images.

File Handling: Familiarize yourself with PHP’s file-handling functions, such as move_uploaded_file.

Uploading the Image

Now, let’s dive into the process of uploading an image to your database:

Create the HTML Form


<div class="container mt-5">
        <div class="row d-flex justify-content-center mt-5">

            <?php success(); ?>
            <div class="col-lg-6 col-md-6 col-sm-12 col-12 mt-5">
                <div class="card shadow shadow-lg">
                    <div class="card-header bg-success ">
                        <h3 class="card-title text-center text-white">Upload image in Database with PHP</h3>


                    <div class="card-body p-4">
                        <form action="process.php" method="post" enctype="multipart/form-data">
                            <div class="mb-3">
                                <input type="file" name="img" class="form-control" required>


                            <div class="mb-3 d-flex justify-content-center">
                            	<input type="submit" value="Upload Image" class="btn btn-outline-primary w-75">









In this form, users can choose an image file to upload.

Handling the Upload in PHP

Create a PHP script (process.php) to handle the file upload:




          $errors= array();
          $img = $_FILES['img']['name'];
          $file_size = $_FILES['img']['size'];
          $file_tmp = $_FILES['img']['tmp_name'];
          $file_type = $_FILES['img']['type'];




    $sql = "INSERT INTO image SET image = '$img'";

    $query = mysqli_query($connection, $sql);

    if ($query) 

       $_SESSION['Success'] = "Image Uploaded Successfully";

        header('Location: index.php');


        echo mysqli_error($connection);



In this script, we retrieve the uploaded image data using $_FILES, and then you can use SQL queries to store it in your database.

Storing the Image in the Database

You will need to create a table in your database to store the image data.

To learn more about enhancing your web applications, check out this tutorial on creating a custom Captcha and contact form in PHP.


Congratulations! You have learned how to upload an image to a database using PHP. This skill is essential for building feature-rich web applications that involve image handling. By following the steps outlined in this guide, you can enhance your web development repertoire and create more dynamic and interactive websites.

Remember that handling file uploads and database queries properly is essential for your web application’s dependability and security. To avoid possible flaws, always verify and sanitize user inputs.

Now that you have the knowledge, go ahead and build amazing web applications with image upload capabilities!

Feel free to explore more PHP and database-related topics to expand your skills further. Happy coding!




Please enter your comment!
Please enter your name here

- Advertisment -
Hire Us