laravel - 检查文件夹是否已存在,如果不存在,则在 laravel 中通过 id 创建一个新文件夹

标签 laravel laravel-5

我想将图像存储到特定文件夹,该文件夹将按页面 ID 命名。例如,如果页面 id=1,则文件夹位置应为 public/pages/page_id/image_here。

如果该文件夹尚不存在,系统将生成并以其页面 ID 命名。

 $id=1;
 $directoryPath=public_path('pages/' . $id);

if(File::isDirectory($directoryPath)){
        //Perform storing
    } else {
        Storage::makeDirectory($directoryPath);
        //Perform storing
    }

但是我遇到错误“mkdir():无效参数”。 我能知道为什么会发生吗?

经过我的研究,有人说文件夹名称应该基于 id+token,这样入侵者就无法根据 id 搜索图像,有可能实现吗?

最佳答案

我也遇到了同样的问题,但是当我使用 File 而不是 Storage 时,它就可以工作了!

$id=1;
$directoryPath=public_path('pages/'.$id);

//check if the directory exists
if(!File::isDirectory($directoryPath)){
    //make the directory because it doesn't exists
    File::makeDirectory($directoryPath);
}

//Perform store of the file

希望这有效!

关于laravel - 检查文件夹是否已存在,如果不存在,则在 laravel 中通过 id 创建一个新文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41424112/

相关文章:

php - MySQL before Trigger,使用查询设置多个字段

php - Laravel:命令中的依赖注入(inject)

php - Eloquent - 在数据库中存储序列化数组 - 方法覆盖

php - 当 REDIS 想要过期时如何处理

laravel - 获取 Laravel 中出现的次数

mysql - Laravel 架构构建器 INT(11) on integer() 和 INT(10) on unsignedInteger()

mysql - 不从表中检索值集。拉维尔

php - 从控制台脚本获取 web php.ini 文件的值

laravel - 如何在邮件 Laravel 中附加存储中的图像

php - Laravel cURL POST 抛出 TokenMismatchException