Laravel图库逻辑

Sid*_*ide 8 php image image-processing laravel

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

我对逻辑有点困惑.所以我的逻辑就是这样.

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

文件夹表

id | folder        | user_id 
1  | Some folder   | 1
2  | New folder    | 4
3  | Nother folder | 7
Run Code Online (Sandbox Code Playgroud)

图像表

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
Run Code Online (Sandbox Code Playgroud)

关系

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

class Image extends Eloquent 
{
    public function folder()
    {
        return static::belongs_to('Folder');
    }
}
Run Code Online (Sandbox Code Playgroud)

服务器上的文件夹结构

- samples
  -user_id
   - folder_id
     - image1
     - image2
     - image3
Run Code Online (Sandbox Code Playgroud)

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

所以我的问题.

  • 在您看来,这是一个很好的逻辑
  • 这会导致将来出现问题
  • 你为这个功能提供了什么

而我最神圣的是两件事.

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

感谢您的帮助

Lau*_*nce 19

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

题:

- Is this a good logic in your opinion
- Can this lead problems in the future
- What would you offer for this functionality
Run Code Online (Sandbox Code Playgroud)

回答:

逻辑上似乎声音-但我很好奇在那里你将存储图像?在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);
         }
    }
}
Run Code Online (Sandbox Code Playgroud)

题:

我认为这将导致一个庞大的数据库,第二是编号的,X时间后的时候会有更多的用户,该ID的会增加,我知道这听起来很奇怪,但由于很多的用户会上传影像会导致巨大id's,我的意思是它可能达到数百万

回答:

根据mySQL功能页面:

我们将MySQL Server与包含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>";
    }
?>
Run Code Online (Sandbox Code Playgroud)