How to upload image in mysql php?

Uploading the image/videos into the database and displaying it using PHP is the way of uploading the image into the database and fetching it from the database. Using the PHP code, the user uploads the image or videos they are safely getting entry into the database and the images should be saved into a particular location by fetching these images from the database.
If any of the websites contain the functionality to upload images/videos with some detail, then by using this code we will upload the image into your database and whether you would like to ascertain what the person has got to be uploaded. And by this code the image which is uploaded that where save in your system where you are given the location.

Approach: Make sure you have XAMPP or WAMP server installed on your machine. In this tutorial, we will be using the WAMP server.

1. Create Database: First, we will create a database named ‘geeksforgeeks‘. You can use your existing database or create a new one.

How to upload image in mysql php?

create database “geeksforgeeks”

2. Create Table: Create a table named ‘image‘. The table contains two fields: 

  • id – int(11)
  • filename – varchar(100)

The id should be in Auto incremented(AI). Your table structure should look like this:

table structure of “image”

Or you can create a table by copying and pasting the following code into the SQL panel of your PHPMyAdmin.

CREATE TABLE IF NOT EXISTS `image` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `filename` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

To do this from SQL panel refer to the following screenshot.

create a table ‘image” from the SQL panel

We will be using Bootstrap here to use Bootstrap’s form control. Below is the code to include the Bootstrap CDN link in the head section of the HTML code.

Creating folder and files:

We will now create a folder named “image“. The files uploaded by the client on the server will be stored in this folder. Create index.php and style.css. Keep your main project folder (for example here.. GeeksForGeeks) in the “C://wamp64/www/“, if you are using WAMP or “C://xampp/htdocs/” folder if you are using the XAMPP server respectively. The folder structure should look like this:

folder structure

Program: Now, we will create an HTML form for uploading image files (you can upload any type of file like .pdf or .mp4) and will display the uploaded image.

  • HTML code: 

HTML

<html>

<head>

    <title>Image Uploadtitle>

    <link rel="stylesheet" type="text/css" href="style.css" />

head>

<body>

    <div id="content">

        <form method="POST" action="" enctype="multipart/form-data">

            <div class="form-group">

                <input class="form-control" type="file" name="uploadfile" value="" />

            div>

            <div class="form-group">

                <button class="btn btn-primary" type="submit" name="upload">UPLOADbutton>

            div>

        form>

    div>

    <div id="display-image">

    php

        $query = " select * from image ";

        $result = mysqli_query($db, $query);

        while ($data = mysqli_fetch_assoc($result)) {

    ?>

        <img src="./image/">

    php

        }

    ?>

    div>

body>

html>

Explanation of PHP code: 

  • We are first selecting the records from the table in the $query variable.
  • Then the $result will execute the query.
  • While loop is used to fetch all the records in the $data to fetch the image from the database.
  • And finally, the fetched images are displayed with the help of the
    How to upload image in mysql php?

    output

    Conclusion: The uploaded image into the database with the PHP code is simple and used for various purposes. The code helps to upload the image and then uploaded the image into the database and can be shown in another folder.
    One thing you should note is that when you are running this program there should be a possibility that the image is not uploaded more than 2 MB because the PHP program has set the default value of uploading an image of 2 MB and posting the image of 8 MB. For exceeding the size of uploading the image you should follow the following steps:
     

    • First, open the C drive, then open the folder WAMP or XAMPP server.
    • Then open the bin folder.
    • Open the PHP version folder (PHP 5.6.31 folder) (KINDLY NOTE THAT IF YOU HAVE ANOTHER VERSION OF PHP YOU SHOULD OPEN THAT ALSO)
    • Then search php.ini. Open it and then search the two variables and change with them. The variables are: 
       
    upload_max_size = 100M
    post_max_filesize = 100M
    • Save with this change and then open 
    C:\wamp64\bin\apache\apache2.4.27\bin
    • and search for the php.ini file. Change the same thing which is above mention.
    • Restart the WAMP or XAMPP server and then run the code.

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    How can I insert image in php?

    PHP File Upload.
    Configure The "php.ini" File. First, ensure that PHP is configured to allow file uploads. ... .
    Check if File Already Exists. Now we can add some restrictions. ... .
    Limit File Size. The file input field in our HTML form above is named "fileToUpload". ... .
    Limit File Type. ... .
    Complete Upload File PHP Script..

    How do you insert images to MySQL and display them using php?

    Insert Image File in MySQL. MySQL has a BLOB (binary large object) data type that can hold a large amount of binary data. ... .
    Create Database Table. ... .
    Database Configuration (dbConfig.php) ... .
    Image Upload Form. ... .
    Store Image File in Database (upload. ... .
    Retrieve image from database (view. ... .
    Conclusion..

    Can we upload image in database?

    Uploading the image/videos into the database and displaying it using PHP is the way of uploading the image into the database and fetching it from the database.