4

我正在制作一个 android 应用程序,它拍摄照片并将图像(作为 base64 编码字符串)推送到 PHP 脚本,从这里我将在 MySQL 数据库中存储有关图像的数据。

将图像存储在数据库中是否明智(因为它是作为 base64 字符串传递的),将其转换回图像并将其存储在文件系统中会更好吗?

4

4 回答 4

4

A base64 encoded image takes too much place (about 33% more than the binary equivalent).

MySQL offers binary formats (BLOB, MEDIUM_BLOB), use them.

Alternatively, most people prefer to store in the DB only a key to a file that the filesystem will store more efficiently, especially if it's a big image. That's the solution I prefer for the long term. I usually use a SHA1 hash of the file content to form the path to the file, so that I have no double storage and that it's easy to retrieve the record from the file if I want to (I use a three level file tree, first two levels being made respectively from the first two characters and the characters 3 and 4 of the hash so that I don't have too many direct child of a directory). Note that this is for example the logic of the git storage.

The advantage of storing them in the DB is that you'll manage more easily the backups, especially as long as your project is small. The database will offer you a cache, but your server and the client too, it's hard to decide a priori which will be fastest and the difference won't be big (I suppose you don't make too many concurrent write).

于 2012-06-05T19:25:09.417 回答
2

我已经完成了这两种方式,每次我回到将二进制数据存储在 MySQL 表中的代码时,我总是使用 MySQL 表中的指针将其切换到文件系统。

在性能方面,使用 FS 会好得多,因为从 MySQL 服务器中提取多个大型 BLOB 会很快使其管道饱和。通常它是您不想堵塞的管道。

于 2012-06-05T19:32:05.973 回答
2

您始终可以将其保存base64_encode($image)在文件中,仅将文件路径存储在数据库中,然后用于fopen()获取编码图像。

如果我没有正确理解这个问题,我深表歉意。

于 2012-06-05T19:45:40.953 回答
0

我认为“明智”是相当主观的。我认为从“阻止人们直接链接到我的图像”的角度来看这是明智的。此外,如果您决定需要更改目录结构等,它可能会有所帮助。它可能会让您更轻松(但这实际上取决于您如何编写脚本开始......)但除此之外......我真的想不出这样做有什么好处。

于 2012-06-05T19:27:09.673 回答