简体   繁体   中英

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.

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.

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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM