Codeigniter:在数据库中存储图像数据的首选方法
我需要将图像数据存储在数据库中。我将图像上传到服务器,然后,当然,我需要从数据库中检索它们。我知道在数据库中我应该只存储数据,将实际文件保留到文件系统,所以我试图思考哪一种可能是保存这些数据的最佳方法。
最困扰我的是选择命名方案。在数据库中,我只有一个 filename
列,其中仅存储文件名和扩展名。没有路径,所以我可以在我的应用程序中配置它。那么:我应该使用什么作为文件名?我想保留一个有意义的名称,因此保留照片标题或至少保留其中的一部分会很酷。但为了确保独特性,哪种方法可能是最好的方法呢?
我知道 Codeigniter 有一个字符串助手,但我也阅读了有关使用 php 函数 uniqid 的信息。然后我可以将 id 附加到图片名称上。
你有什么建议?
I need to store images data in a database. I'm uploading images to a server and then, of course, I need to retrieve them from the database. I know that in the database I should only store just datas leaving the actual files to the filesystem so I'm trying to think which one may be the best way to save those datas.
What's bothering me the most is choosing a naming scheme. In the database I'll just have a filename
column, where to store just the filename plus the extension. No path, so I can config it in my application. So: what shall I use for the filename? I'd like to keep a meaningful name, so it would be cool to keep the photo title, or at least a part of it. But to ensure uniqueness which one could be the best approach?
I know Codeigniter has a string helper, but I also read about using the php function uniqid. Then I can attach the id to the picture name.
What do you suggest?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以随意将整个映像存储在数据库中,只要不会有数十万个映像(或者您只是喜欢为昂贵的 SCSI 驱动器付费:))。如果您不确定,文件系统也很好。
对于图像的唯一ID,最好的方法可能是只使用数据库插入ID(保证唯一,没有线程/冲突/哭泣)。所以这是一个很好的方法,假设您想将图像保留在文件系统中:
那么您可能需要将 image_id 与某种分类或标签、user_id 等连接起来。
编辑
添加了评论:
如果您愿意,您始终可以使用真实文件名构建链接,即使您将它们简单地存储为文件系统中的图像 ID。您只需要利用搜索友好的网址来执行类似的操作(其中1234是图像ID和/ images/ 重定向到您的 php 脚本):
然后您只需将 /images/db/ 重定向到您的 php 脚本。或者,如果您不想尝试重写它们,则可以仅使用 $PATH_INFO。
例如,如果您有 mysite.com/images/render.php/1234/picture_of_cat.jpg:
祝您好运! ;)
Feel free to store the entire image in your database, as long as there won't be hundreds of thousands of them (or you just like paying for expensive SCSI drives :) ). If you're unsure, the file system is fine too.
For the image's unique id, the best approach is probably to just use the DB insert id (guaranteed unique, no threading/clashing/crying). So here's a good approach, assuming you want to keep the images in the file system:
Then you'll probably want to connect image_id with some sort of categorization or tags, a user_id, etc.
EDIT
Added for comments:
You can always build your links using the real file name if you like, even if you store them as simply the image id in your file system. You just need to utilize search friendly urls to do something like this (where 1234 is the image id and /images/ redirects to your php script):
Then you just redirect /images/db/ to your php script. Alternately, if you don't want to try rewriting them, you can just use $PATH_INFO.
For example, if you have mysite.com/images/render.php/1234/picture_of_cat.jpg:
Good luck! ;)
好吧,我更喜欢在图像名称中使用哈希,因为如果有两个图像同名,就会发生这种情况,并且会产生未来的问题。
另一个提示,永远不要将文件的内容存储在数据库中,其成本比仅存储该文件的路径要高得多。
Well, I prefer that you use a hash in the name of the image, as can happen in case there are two images of the same name, and will generate a future problem.
Another tip, never want to stores the contents of a file in the database, the cost is much higher than just store the path to that file.
将图像存储在数据库中不是一个好主意,而是将图像的链接存储在数据库中。
例如;
创建一个包含以下字段的表
您可以添加任何您想要的其他字段
Storing images in a database is not a good idea, instead store the links to the images in the database.
For example;
create a table with the following fields
You can add any other field you want