简体   繁体   English

图库系统 - 哪种方法更好?

[英]Image Gallery System - Which Approach is Better?

I am implementing an image upload system in PHP, The following are required: 我正在用PHP实现图像上传系统,需要以下内容:

  • Have categories 有类别
  • Allow users to comment on images 允许用户对图像发表评论
  • Allow rating of images 允许图像评级

For that, I have 2 approaches in mind: 为此,我有两种方法:

1. Implement the categorization by folders 1.按文件夹实施分类

Each category will have its own folder, and PHP will detect categories via those folders. 每个类别都有自己的文件夹,PHP将通过这些文件夹检测类别。

Pros 优点

  • Structured look, easily locatable images. 结构化外观,易于定位的图像。
  • Use of native PHP functions to manipulate and collect information about folders and files 使用本机PHP函数来操作和收集有关文件夹和文件的信息

Cons 缺点

  • Multiple categorization is a pain 多重分类是一种痛苦
  • Need to save the full path in the database 需要保存数据库中的完整路径

2. Implement the categorization by database 2.按数据库实施分类

Each image in the database will have a catID (or multiple catIDs), and PHP will query the database to get the images 数据库中的每个图像都有一个catID(或多个catID),PHP将查询数据库以获取图像

Pros 优点

  • Easily implemented multi-categories 轻松实现多类别
  • Only image name is saved 仅保存图像名称

Cons 缺点

  • Seems more messy 似乎更混乱
  • Need to query the database a lot. 需要查询数据库很多。

Which do you think is better? 你认为哪个好些? Or is there a third, completely different, approach that I'm missing? 或者是否存在我缺少的第三种完全不同的方法?

Just a note, I don't need code, I can implement that myself, I'm looking to find what to implement. 只是一个注释,我不需要代码,我可以自己实现,我正在寻找实现的内容。

Would love to hear from you. 很想听听你的意见。

I believe that the second option is better, a DB is giving you much more flexibility, and I think better performance then file system, if you set the right indexes. 我相信第二个选项更好,DB会给你更大的灵活性,如果设置正确的索引,我认为文件系统的性能会更好。

In the filesystem approach you are limited to only 1 category per image, when in the DB you can set multiple categories on an image. 在文件系统方法中,每个图像仅限于1个类别,在数据库中,您可以在图像上设置多个类别。

The con that Db is more messy, sorry I can't find a reason way in the db it will be more messy, maybe you mean that the files are not organized on the file system, but you still need to organize the files on the file system and divide them to multiple folders for better performance, and if you want to get all the images that have been uploaded you query the db for all of them, which will be much faster then ls on all the categories folders. 认为Db更乱的骗局,对不起我在db中找不到合理的方式会比较麻烦,也许你的意思是文件系统没有组织文件,但是你仍然需要组织文件文件系统并将它们分成多个文件夹以获得更好的性能,如果你想获得所有已经上传的图像,你可以查询所有这些文件的数据库,这将比所有类别文件夹上的ls快得多。
In organize the files in the file system when using the DB approach I mean that you need to divide them to several folders, actually it depends on how you predict the upload of the images will be: 在使用数据库方法时组织文件系统中的文件我的意思是你需要将它们分成几个文件夹,实际上它取决于你预测图像上传的方式:

  1. If you predict that the upload will be spread on long time then I think that better to put the files in directories per range on time(day, week, month) example if I upload an image now it will go to "/web_path/uploaded_photos/week4_2012/[some_generated_string].jpg" 如果你预测上传会在很长一段时间内传播,那么我认为最好将文件按时间(日,周,月)放在目录中。例如,如果我现在上传图像,它将转到“/ web_path / uploaded_photos /week4_2012/[some_generated_string].jpg”
  2. If you don't know how to predict the uploads, then I suggest you will divide the files into folders on something generic like the first two letters in MD5 hash on the image name, for example if my file name is "photo_2012.jpg" the hash will be "c02d73bb3219be105159ac8e38ebdac2" so the path in the files system will be "/web_path/uploaded_photos/c/0/[some_generated_string].jpg" 如果您不知道如何预测上传,那么我建议您将文件分成一些通用的文件夹,如图像名称上的MD5哈希中的前两个字母,例如,如果我的文件名是“photo_2012.jpg”哈希值为“c02d73bb3219be105159ac8e38ebdac2”,因此文件系统中的路径为“/web_path/uploaded_photos/c/0/[some_generated_string].jpg”

The second con that need to query the DB a lot is not quite true, cause you will need the same amount of queries on the file system which are far more slower. 需要经常查询数据库的第二个问题并不完全正确,因为在文件系统上需要相同数量的查询速度要慢得多。

Good luck. 祝好运。

PS Don't you forget to generate a new file name to any image that have been uploaded so there will be no collisions in different users uploaded same image name, or the same user. PS不要忘记为任何已上传的图像生成新文件名,以便不同用户上传相同图像名称或同一用户时不会发生冲突。

I'd be inclined to go with the database approach. 我倾向于采用数据库方法。 You list the need to query the database a lot as a con, but that's what databases are built for. 您列出了将数据库作为con进行查询的必要性,但这就是为数据库构建的内容。 As you pointed out yourself, a hierarchical structure has serious limitations when it comes to items that fall into more than one category, and while you can use native PHP functions to navigate the tree, would that really be quicker or more efficient than running SQL queries? 正如您自己指出的那样,当涉及到属于多个类别的项目时,层次结构具有严重的局限性,并且虽然您可以使用本机PHP函数来导航树,但这比运行SQL查询真的更快或更有效?

Naturally the actual file data needs to go somewhere, and BLOBS are problematic to put it mildly, so I'd store the actual files in the filesystem, but all the data about the images (the metadata) would be better off in a database. 当然,实际的文件数据需要去某处,并且BLOBS有点问题,所以我将实际文件存储在文件系统中,但是关于图像(元数据)的所有数据在数据库中会更好。 The added flexibility the database gives you is worth the work involved. 数据库为您提供的额外灵活性值得所涉及的工作。

The second solution (database) is actually a TAG/LABEL system of categorizing data. 第二种解决方案(数据库)实际上是对数据进行分类的TAG / LABEL系统。 And that is the way to go, biggest examples being Gmail and Stackoverflow. 这就是要走的路,最大的例子是Gmail和Stackoverflow。 Only thing you need to be careful about is how to model tags. 您唯一需要注意的是如何建模标签。 If the tags are not normalized properly, querying from database becomes expensive. 如果标签未正确标准化,则从数据库查询会变得昂贵。

Use folders only to make file storage reliable, storing certain amount of files per folder, ie 仅使用文件夹使文件存储可靠,每个文件夹存储一定数量的文件,即

/b/e/beach001.jpg

as for your dilemma, it is not a question at all. 至于你的困境,这根本不是一个问题。
From your conditions you can say it yourself that database is the only solution. 根据您的条件,您可以自己说数据库是唯一的解决方案。

Since you need a database to store comments and ratings, you should store categories in database as well. 由于您需要一个用于存储注释和评级的数据库,因此您还应该将类别存储在数据库中。 Sometime later you may also want to store image captions and description; 以后你可能还想存储图像标题和描述; database allows you to do that. 数据库允许您这样做。 And I would not worry about querying the database a lot. 而且我不会担心很多时候查询数据库。

Whether to store the image itself in database or filesystem is a separate issue which is discussed here . 是否将图像本身存储在数据库或文件系统中是一个单独的问题,在此处讨论

Note about storing images in filesystem: do not store thousands of images in a single directory; 关于在文件系统中存储图像的注意事项:不要在单个目录中存储数千个图像; it could cause performance issues for the OS. 它可能会导致操作系统出现性能问题。 Instead invent a way to organize images in sub directories. 而是发明了一种在子目录中组织图像的方法。 You can group them by dates, filenames, randomly etc. Some conventions: 您可以按日期,文件名,随机等对它们进行分组。一些约定:

upload date: month/year 上传日期:月/年

/uploaded_images
    /2010/01
    /2010/02

upload date: month-year 上传日期:月 - 年

/uploaded_images
    /2010-01
    /2010-02

md5 hash of image name: first character 图像名称的md5哈希:第一个字符

/uploaded_images
    /0/
    /1/
    .
    .
    .
    /e/
    /f/

batches of thousands 成千上万的批次

/uploaded_images
    /00001000/
    /00002000/
    /00003000/

I eventually went with the best answer of this question: Effeciently storing user uploaded images on the file system . 我最终得到了这个问题的最佳答案: 有效地将用户上传的图像存储在文件系统上

It works like a charm. 它就像一个魅力。 Thanks for all of the answers! 谢谢你的所有答案!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM