【问题标题】:Laravel image gallery logicLaravel 图片库逻辑
【发布时间】:2013-01-20 05:31:38
【问题描述】:

我最近开始开发一个相当大的网站。 在网站上,我想允许用户上传他们的样本作品。 我们目前非常有限,因此图像将存储在我们的服务器上。

我有点坚持逻辑。 所以我的逻辑是这样的。

用户创建一个文件夹,其名称存储在数据库中,并附有users id

文件夹表

id | folder        | user_id 
1  | Some folder   | 1
2  | New folder    | 4
3  | Nother folder | 7

图片表格

id | image_name        | folder_id |
1  | image1.jpg        | 1
2  | image2.jpg        | 1
3  | image3.jpg        | 1
4  | image4.jpg        | 2
5  | image5.jpg        | 2
6  | image6.jpg        | 2

关系

class Folder extends Eloquent 
{
    public function images()
    {
        return static::has_many('Images');
    }
}

class Image extends Eloquent 
{
    public function folder()
    {
        return static::belongs_to('Folder');
    }
}

服务器上的文件夹结构

- samples
  -user_id
   - folder_id
     - image1
     - image2
     - image3

如您所见,用户创建了一个文件夹,创建文件夹后,user 将图像名称上传到带有folders id 的数据库中,并显示图像将是上面描述的方式与现实.

所以我的问题。

  • 您认为这是一个好的逻辑吗
  • 这会导致将来出现问题
  • 你想为这个功能提供什么

而我最神圣的是两件事。

我认为这会导致一个巨大的数据库,其次是id's,在x 时间之后会有更多用户,id's 会增加,我知道这听起来很奇怪,但是因为很多用户上传图片会导致id很大,我的意思是可能会达到数百万,有没有办法解决这个问题?

感谢您的帮助

【问题讨论】:

  • 我有相同的表,但我希望任何登录用户都可以查看其他用户的所有图像,但只能更新(编辑)和删除他/她自己的。如果有人有,请指导我解决方案。我被困在这里了。

标签: php image image-processing laravel


【解决方案1】:

好的 - 让我们把它分解成几个子答案;

问题:

- Is this a good logic in your opinion
- Can this lead problems in the future
- What would you offer for this functionality

答案:

逻辑似乎听起来 - 但我很好奇在哪里您将存储图像?在 public_html 内部 - 还是在 Web 根目录之外?如果您在 public_html 中有图像 - 并允许浏览器直接访问它们,它将允许用户“猜测”其他用户文件夹并访问它们。您需要安全地存储数据。

要在 webroot 之外制作图像,并确保只有授权用户可以访问它们 - 您应该使用 readfile()。像这样的东西可以解决问题

function user_file($file_name = "")
{
    if ($file_name)
    {
         // Ensure no funny business names to prevent directory transversal etc.
         $file_name = str_replace ('..', '', $file_name);
         $file_name = str_replace ('/', '', $file_name);

         // now do the logic to check user is logged in
         if (Auth::check())
         {
                // Serve file via readfile() - we hard code the user_ID - so they
                // can only get to their own images
               readfile('../your_app/samples/'.Auth::user()->id.'/'.$file);
         }
    }
}

问题:

我认为这会导致一个巨大的数据库,其次是 id,x 次之后会有更多的用户,id 会增加,我知道这听起来很奇怪,但是由于很多用户会上传图片导致巨大的身份,我的意思是它可能会达到数百万

答案:

根据mySQL features page

我们将 MySQL 服务器与包含 5000 万条记录的数据库一起使用。我们还知道使用 MySQL 服务器的用户有 200,000 个表和大约 5,000,000,000 行。

所以这是 50 亿行。你可能会得到几百万。所以你在这里是安全的(取决于你的硬件)。

问题:

...但是由于很多用户会上传图片会导致巨大的ID, 我的意思是它可能会达到数百万,有没有办法 解决这个问题?

答案:

如果您不想存储数百万条记录,并且担心性能,一种选择是保留文件夹表,但删除图像表。相反,您可以在文件夹上使用scandir() - 并让 PHP 从目录本身检索文件名。那么你就没有那么多开销了。

<?php
    $list_of_user_files = scandir("$user_id/$folder_id");
    foreach ($list_of_user_files as $file) {
          echo "File: $file <br>";
    }
?>

【讨论】:

  • 感谢您的详细回答,图片将存储在存储文件夹中,是的,我知道,对此进行了保护:)
【解决方案2】:

存储文件夹表和使用scandir函数的方法是一个标准的过程。并允许 php 从文件夹中检索文件名。如果您有许多文件,请尝试使用 wordpress 中的年月顺序对它们进行分类。 喜欢

2012
  01
  02
  03
2013
  01
  02
  03 

文件夹ID内的等。所以一个文件夹中的图片总数会比较少。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-29
    • 1970-01-01
    • 2017-09-13
    • 2020-01-22
    • 2014-10-24
    • 2014-10-06
    • 2018-10-15
    相关资源
    最近更新 更多