您可以随意将整个映像存储在您的数据库中,只要不会有数十万个(或者您只是喜欢为昂贵的 SCSI 驱动器付费 :))。如果您不确定,文件系统也可以。
对于图像的唯一 ID,最好的方法可能是只使用 DB 插入 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 等联系起来。
编辑
为 cmets 添加:
如果您愿意,您始终可以使用真实文件名构建链接,即使您将它们作为简单的图像 ID 存储在文件系统中也是如此。你只需要使用search friendly urls 来做这样的事情(其中 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']);
祝你好运! ;)