You can save your uploading images in the database table for later use e.g. display user profile or product image, create the image gallery, etc.

Đang xem: Image upload using php and mysql database

There are two ways of doing this –

Save the path or name of an imageEncode image into a base64 format

In this tutorial, I show you both of the methods for storing and retrieving an image from the database table.

*

Contents

1. Table structure

In the example, I am using images table for storing data.

name – This field is used to store the image file name.image – This field is used to store the image base64 generated value.

CREATE TABLE `images` ( `id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT, `name` varchar(200) NOT NULL, `image` longtext NOT NULL) ENGINE=InnoDB DEFAULT CHARSET=latin1;

2. Configuration

Create a new config.php file for database configuration.

Completed Code

3. Save path or name

You can either save the full path or name of an image in your MySQL database table. Retrieve the image name or path from the MySQL database and use it to make an image source.

Xem thêm:

Here, I am storing the file name in the MySQL database.

Completed Code

Retrieve

Select the name or path of the image which you have stored in the database table and use it in the image source.

Example

*

Now when you need to display the image just fetch the value and use it as an image source.

Note – In the example, I upload the image to directory. You can remove the upload code if you only want the image will accessible through base64 stored values in the database.

Completed Code

Retrieve

Select the stored base64 value and using it in the image source.

Xem thêm: Create Php Mvc App Lication Pages With Mvc And Bootstrap &Ndash; Self

Example

*

Are you want to get implementation help, or modify or extend the functionality of this script? Submit paid service request.

Related Post

Leave a Reply

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