Laravel 图片库逻辑
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14704998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel image gallery logic
提问by Side
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.
我有点坚持逻辑。所以我的逻辑是这样的。
User creates a folder with a name that is stored in the database with the users id
attached to it
用户创建一个文件夹,其名称存储在数据库中,users id
并附有
folder table
文件夹表
Rows
行
id | folder | user_id
1 | Some folder | 1
2 | New folder | 4
3 | Nother folder | 7
Images table
图像表
Rows
行
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
Relations
关系
class Folder extends Eloquent
{
public function images()
{
return static::has_many('Images');
}
}
class Image extends Eloquent
{
public function folder()
{
return static::belongs_to('Folder');
}
}
folder structure on server
服务器上的文件夹结构
- samples
-user_id
- folder_id
- image1
- image2
- image3
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.
如您所见,用户创建了一个文件夹,创建文件夹后,user
将图像名称上传到数据库中folders id
,并且显示图像将按照上述方式进行显示。
So my questions.
所以我的问题。
- Is this a good logic in your opinion
- can this lead problems in the future
- what woud you offer for this functionality
- 你认为这是一个很好的逻辑吗
- 这会导致未来出现问题吗
- 你会为这个功能提供什么
And what i am most sacred of are 2 things.
我最神圣的是两件事。
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?
我认为这会导致一个巨大的数据库,其次是id's
,在 x 时间之后会有更多用户,id's
将会增加,我知道这听起来很奇怪,但由于很多用户上传图片会导致巨大的 id,我的意思是它可能会达到数百万,有没有办法解决这个问题?
Thank you for the help
感谢您的帮助
回答by Laurence
Ok - lets break this down into a few sub-answers;
好的 - 让我们把它分解成几个子答案;
Question:
题:
- Is this a good logic in your opinion
- Can this lead problems in the future
- What would you offer for this functionality
Answer:
回答:
The logic seems sounds - but I'm curious whereyou 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.
逻辑似乎听起来 - 但我很好奇你将在哪里存储图像?在 public_html 内部 - 还是在 web 根目录之外?如果您在 public_html 中有图像 - 并允许浏览器直接访问它们,它将允许用户“猜测”其他用户文件夹并访问它们。您需要安全地存储数据。
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
要在 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);
}
}
}
Question:
题:
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
我认为这会导致一个巨大的数据库,其次是 id,在 x 时间之后会有更多用户时,id 会增加,我知道这听起来很奇怪,但是由于很多用户会上传图片会导致巨大的id,我的意思是它可能会达到数百万
Answer:
回答:
According to the mySQL features page:
根据mySQL 功能页面:
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.
我们将 MySQL Server 与包含 5000 万条记录的数据库一起使用。我们还知道使用 MySQL Server 的用户有 200,000 个表和大约 5,000,000,000 行。
So thats 5 billion rows. You will maybe get to a few million. So you are safe here (depending upon your hardware).
所以这是 50 亿行。你可能会达到几百万。所以你在这里很安全(取决于你的硬件)。
Question:
题:
...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?
...但是由于很多用户上传图片会导致巨大的ID,我的意思是它可能会达到数百万,有没有办法解决这个问题?
Answer:
回答:
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.
如果您不想存储数百万条记录,并且担心性能,那么一种选择是保留文件夹表,而删除图像表。相反,您可以在文件夹上使用scandir()- 并让 PHP 从目录本身检索文件名。那么你没有那么多的开销。
<?php
$list_of_user_files = scandir("$user_id/$folder_id");
foreach ($list_of_user_files as $file) {
echo "File: $file <br>";
}
?>
回答by Ranjith Siji
The metode of Storing the folder table and using the scandir function is a standared procedure. And allow php to retrive the file names from the folder. If you have a number of files then try categorizing them with year and month order like in wordpress. Like
存储文件夹表和使用 scandir 函数的方法是标准程序。并允许 php 从文件夹中检索文件名。如果您有多个文件,请尝试像在 wordpress 中一样按年和月顺序对它们进行分类。喜欢
2012
01
02
03
2013
01
02
03
etc inside the folder id. So the total number of images in a folder will be comparatively less.
文件夹 id 内的等。所以一个文件夹中的图像总数会相对较少。