Codeigniter:在数据库中存储图像数据的首选方法

发布于 2024-12-19 10:20:16 字数 374 浏览 2 评论 0原文

我需要将图像数据存储在数据库中。我将图像上传到服务器,然后,当然,我需要从数据库中检索它们。我知道在数据库中我应该只存储数据,将实际文件保留到文件系统,所以我试图思考哪一种可能是保存这些数据的最佳方法。

最困扰我的是选择命名方案。在数据库中,我只有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

爱给你人给你 2024-12-26 10:20:16

您可以随意将整个映像存储在数据库中,只要不会有数十万个映像(或者您只是喜欢为昂贵的 SCSI 驱动器付费:))。如果您不确定,文件系统也很好。

对于图像的唯一ID,最好的方法可能是只使用数据库插入ID(保证唯一,没有线程/冲突/哭泣)。所以这是一个很好的方法,假设您想将图像保留在文件系统中:

create table images (
   image_id int(20) primary key auto_increment, /* assumes mysql */
   file_path varchar(2500) not null, /* the actual file name and full path */
   file_label varchar(255), /** user's memorable name for the file */
   description varchar(2500), /** user provided description of contents */
   media_type varchar(100), /** something like image/jpeg; privided by php in upload */
);

那么您可能需要将 image_id 与某种分类或标签、user_id 等连接起来。

编辑

添加了评论:

如果您愿意,您始终可以使用真实文件名构建链接,即使您将它们简单地存储为文件系统中的图像 ID。您只需要利用搜索友好的网址来执行类似的操作(其中1234是图像ID和/ images/ 重定向到您的 php 脚本):

mysite.com/images/db/1234/picture_of_a_cat.jpg)

然后您只需将 /images/db/ 重定向到您的 php 脚本。或者,如果您不想尝试重写它们,则可以仅使用 $PATH_INFO。

例如,如果您有 mysite.com/images/render.php/1234/picture_of_cat.jpg:

<?php
$parts = explode("/",$PATH_INFO);
$image_id = $parts[3];
$image_from_db = null; //fetch the row from your dabatase here!
header("Content-type: ".$image_from_db['media_type']);
echo file_get_contents($image_from_db['file_path']);

祝您好运! ;)

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:

create table images (
   image_id int(20) primary key auto_increment, /* assumes mysql */
   file_path varchar(2500) not null, /* the actual file name and full path */
   file_label varchar(255), /** user's memorable name for the file */
   description varchar(2500), /** user provided description of contents */
   media_type varchar(100), /** something like image/jpeg; privided by php in upload */
);

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):

mysite.com/images/db/1234/picture_of_a_cat.jpg)

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:

<?php
$parts = explode("/",$PATH_INFO);
$image_id = $parts[3];
$image_from_db = null; //fetch the row from your dabatase here!
header("Content-type: ".$image_from_db['media_type']);
echo file_get_contents($image_from_db['file_path']);

Good luck! ;)

暗地喜欢 2024-12-26 10:20:16

好吧,我更喜欢在图像名称中使用哈希,因为如果有两个图像同名,就会发生这种情况,并且会产生未来的问题。

另一个提示,永远不要将文件的内容存储在数据库中,其成本比仅存储该文件的路径要高得多。

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.

海之角 2024-12-26 10:20:16

将图像存储在数据库中不是一个好主意,而是将图像的链接存储在数据库中。
例如;
创建一个包含以下字段的表

  • image_id // 图像的唯一 ID
  • image_type // 图像类型("png","jpg","gif".....)
  • image_link // 图像所在文件夹的路径
  • upload date // 图像上传日期(对于数据库查询中的排序很有用)

您可以添加任何您想要的其他字段

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

  • image_id //unique id of the image
  • image_type // type of image("png","jpg","gif".....)
  • image_link // path to folder where the image is located
  • upload date // date of upload of image(usefull for ordering in database queries)

You can add any other field you want

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文