Vivek Kumar
Vivek Kumar

Reputation: 1282

What is proper way to upload a image to a MySQL database?

What is the proper way to upload images into a MySQL database?

Generally, I declare the 'image' field as text and I use move_uploaded_file() function in order to save images on my server.

Is this the correct way to upload images into a database? I will have to upload lot of images, as social networking website might have to, for instance.

Upvotes: 2

Views: 522

Answers (3)

http8086
http8086

Reputation: 1686

As pointed out, it is not a good idea to save the image directly into database compared to save image on filesystem then save path/link to table fields.

but since the question is "upload images into mysql database"

I think you can check http://www.mysqltutorial.org/php-mysql-blob/

Upvotes: 0

Guilherme Soldateli
Guilherme Soldateli

Reputation: 84

I think there are two reliable ways to work with images into your database:

1 - Store the file in some folder at your server, then write the filepath in some field in your image table.

2 - Encode your image as base64 with the base64_encode($yourImageData) method. This method will return a string that can be inserted into your table.

Upvotes: 3

user2672373
user2672373

Reputation:

Preferably upload the image to server using File handling and PHP image libraries like GD and then store path as string in database. I think that should work.

Upvotes: 2

Related Questions