简体   繁体   English

将映像上传到MySQL数据库的正确方法是什么?

[英]What is proper way to upload a image to a MySQL database?

What is the proper way to upload images into a MySQL database? 将图像上传到MySQL数据库的正确方法是什么?

Generally, I declare the 'image' field as text and I use move_uploaded_file() function in order to save images on my server. 通常,我将“图像”字段声明为文本,并使用move_uploaded_file()函数将图像保存在服务器上。

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. 1-将文件存储在服务器上的某个文件夹中,然后在图像表的某个字段中写入文件路径。

2 - Encode your image as base64 with the base64_encode($yourImageData) method. 2-使用base64_encode($ yourImageData)方法将图像编码为base64。 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. 最好使用文件处理和PHP图像库(如GD)将图像上传到服务器,然后将路径作为字符串存储在数据库中。 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" 但由于问题是“将图像上传到mysql数据库”

I think you can check http://www.mysqltutorial.org/php-mysql-blob/ 我认为您可以检查http://www.mysqltutorial.org/php-mysql-blob/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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