我想将图像存储到特定文件夹,该文件夹将按页面 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/