Laravel图库逻辑 [英] Laravel image gallery logic

查看:101
本文介绍了Laravel图库逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I recently started to develop a pretty huge site. On the site i would like to allow users to upload their sample works. We are pretty limited at the moment so the images will be stored on our server.

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

I am a bit stuck with the logic. So my logic would be this.

用户使用用户id <创建一个名称存储在数据库中的文件夹/ code>附加到它

User creates a folder with a name that is stored in the database with the users id attached to it

文件夹表

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 使用文件夹id 将图像名称上传到数据库中,并显示图像将按照上面描述的方式进行显示。

so as you can see, user creates a folder, after the folder is created, user uploades the image name in to the database with the folders id, and showing the images would be the way describe above with the realation.

所以我的问题。


  • 你认为这是一个很好的逻辑

  • 这会导致将来出现问题

  • 您为此功能提供的内容

我最神圣的是两件事。

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

I think this will lead to a huge database, second are the id's, after x time when there will be more users, the id's will increase, and i know this will sound strange, but since lot of users will upload images will lead to huge id's, what i mean by this it will maybe reach millions, is there a way to solve this problem?

谢谢你的帮助

推荐答案

好的 - 让我们将其分解为几个子答案;

Ok - lets break this down into a few sub-answers;

问题:

- 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中有图像 - 并允许浏览器直接访问它们,它将允许用户猜测其他用户文件夹并访问它们。您需要安全地存储数据。

The logic seems sounds - but I'm curious where you will store the images? Inside public_html - or outside the web root? If you have the images inside public_html - and allow the browser to access them directly, it will allow users to 'guess' other user folders and access those. You need to store the data securely.

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

To make images outside the webroot, and make sure only authorized users can access them - you should use readfile(). Something like this will do the trick

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会增加,我知道这听起来很奇怪,但是因为很多用户上传图片会导致巨大的id,我的意思是这可能会达到数百万美元

I think this will lead to a huge database, second are the id's, after x time when there will be more users, the id's will increase, and i know this will sound strange, but since lot of users will upload images will lead to huge id's, what i mean by this it will maybe reach millions

答案:

根据 mySQL功能页面


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

We use MySQL Server with databases that contain 50 million records. We also know of users who use MySQL Server with 200,000 tables and about 5,000,000,000 rows.

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

So thats 5 billion rows. You will maybe get to a few million. So you are safe here (depending upon your hardware).

问题:


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

...but since lot of users will upload images will lead to huge id's, what i mean by this it will maybe reach millions, is there a way to solve this problem?

答案:

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

If you dont want to store millions of records, and your worried about performance, one option is to keep the folder table, but drop the image table. Instead you can use scandir() on the folder - and get PHP to retrieve the file names from the directory itself. Then you dont have as much overhead.

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

这篇关于Laravel图库逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆