好的 - 让我们将其分解为几个子答案;
问题:
- Is this a good logic in your opinion
- Can this lead problems in the future
- What would you offer for this functionality
Answer:
这个逻辑似乎听起来很合理——但我很好奇where你会存储图像吗?在 public_html 内部 - 还是在网络根目录外部?如果您在 public_html 中有图像 - 并允许浏览器直接访问它们,它将允许用户“猜测”其他用户文件夹并访问这些文件夹。您需要安全地存储数据。
要在 webroot 之外制作图像,并确保只有授权用户才能访问它们 - 您应该使用读取文件()。像这样的事情就可以解决问题
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,我的意思是它可能会达到数百万
Answer:
根据mySQL 功能页面:
我们使用 MySQL Server 和包含 5000 万条记录的数据库。我们还知道使用具有 200,000 个表和大约 5,000,000,000 行的 MySQL Server 的用户。
这就是 50 亿行。你可能会达到几百万。所以你在这里是安全的(取决于你的硬件)。
问题:
...但是由于很多用户会上传图像会导致巨大的 ID,
我的意思是它可能会达到数百万,有没有办法
解决这个问题?
Answer:
如果您不想存储数百万条记录,并且担心性能,一种选择是保留文件夹表,但删除图像表。相反,你可以使用扫描目录()在文件夹上 - 并让 PHP 从目录本身检索文件名。那么你就没有那么多的开销了。
<?php
$list_of_user_files = scandir("$user_id/$folder_id");
foreach ($list_of_user_files as $file) {
echo "File: $file <br>";
}
?>