网站照片库
使用照片库时,是否有一种“标准”方式在网络上存储照片? 将照片存储在 mysql 数据库中是不好的做法吗?我应该将照片存储在文件夹中,并将链接存储在数据库中吗?
当谈论存储照片以在网站上使用时,什么是“良好实践”?
Is there a "standard" way of storing photos on the web, when working with photo galleries?
Is it bad practice to store photos in a mysql database? Should I store the photo in a folder, and store the link in a database?
What is "good practice" when talking about storing photos, to use on a website?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用不同的 BLOB 数据类型将照片存储在数据库中。如果照片数量过多或过大,数据库的性能可能会受到影响,并且大小会急剧增加。本质上,如果您有 100 张 10Mb 的照片,那么数据库将增长 1,000Mb 或大约 1GB 的大小。备份将花费很长时间才能执行,如果包含图像的表由于某种原因而损坏,这将导致比您愿意处理的更令人头痛的事情。
正如 imoda 建议的那样,我的建议是将它们存储在硬盘上,并在数据库中链接到它们,您将在数据库中保存所有元数据和文件位置的链接。最佳做法是将照片的文件名存储在数据库中,并重命名该文件以使用主键的值作为文件名。例如,第 5 行与照片“x”关联,其中 x 是文件名,将其存储在硬盘上驱动器并将照片重命名为文件名 5,然后当您访问它进行下载等操作时,您将从数据库中的第 5 行提取实际文件名,并在下载之前重命名它。如果您碰巧上传两个或多个具有相同文件名的文件,这将防止意外覆盖该文件。
You can store photos in your database using the different BLOB data types. If the photos are excessive in quantity, or extra large, the database my start to take a performance hit and will increase in size drastically. Essentially, if you have 100 10Mb photos, that's 1,000Mb or around a Gb of size that the database will grow. Backups will take forever to execute, and if a table containing the images corrupts for whatever reason, it'll induce more headache than you'll care to deal with.
My suggestion as imoda recommends is to store them on a hard drive, and link to them in the database where you'll hold all of the metadata and a link to the location of the file. Best practice is to store the photo's file name in the database, and rename the file to use the primary key's value as the file name As an example, row 5 associates to photo "x" where x is the filename, store it on the hard drive and rename the photo to the file name of 5, and then when you go to access it for things like download, you'll pull the actual file name from row 5 in the database and rename it before you download it. That'll keep from accidentally overwriting the file if you happen to upload two or more files with the same file name.
99% 的情况下您不想将照片存储在 MySQL 数据库中。如果处理不当,将会非常耗费资源。将照片存储在网站的目录中,并将它们的路径存储在数据库中。
99% of the time you don't want to store a photo in a MySQL database. It's incredibly resource intensive if done improperly. Store your photos in directories on your website and store the path to them in your database.
我建议使用 Flickr 添加/删除并重新排列您的图片。使用 Flickr 的优点是您不必编写任何复杂的代码或托管图片。您可以使用 PHP 和 Flickr API 通过您的个人资料 ID 检索图片。
1. 使用 Flickr 创建应用程序
2.下载phpFlickr并将其放在服务器的根目录下
https://code.google.com/p/phpflickr/downloads/list
将此 PHP 代码添加到您的 gallery.php 文件
这是带有分页功能的 PHP Gallery 的代码
I suggest to use Flickr to add/Remove et rearrange your picture. The advantage of using Flickr is that you don't have to write any complex code or host the pictures. You can use PHP and the Flickr API to retrieve the pictures using your profile ID.
1. Create an App with Flickr
2. Download phpFlickr and put it on the root of your server
https://code.google.com/p/phpflickr/downloads/list
add this PHP code to your gallery.php file
Here is the code for the PHP Gallery with Pagination