How to insert image in 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.

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

    Image Upload

    

    

        

            

                

            

            

                UPLOAD

            

        

    

    

    

        

Chủ Đề