正在检查文件夹是否已存在,如果不存在,请在laravel中按id创建新文件夹
我想将我的图像存储到一个特定文件夹中,该文件夹将按页面id命名。例如,如果页面id=1,则此处的文件夹位置应为public/pages/page\u id/image\u 如果文件夹尚不存在,系统将生成并使用其页面id命名正在检查文件夹是否已存在,如果不存在,请在laravel中按id创建新文件夹,laravel,laravel-5,Laravel,Laravel 5,我想将我的图像存储到一个特定文件夹中,该文件夹将按页面id命名。例如,如果页面id=1,则此处的文件夹位置应为public/pages/page\u id/image\u 如果文件夹尚不存在,系统将生成并使用其页面id命名 $id=1; $directoryPath=public_path('pages/' . $id); if(File::isDirectory($directoryPath)){ //Perform storing } else {
$id=1;
$directoryPath=public_path('pages/' . $id);
if(File::isDirectory($directoryPath)){
//Perform storing
} else {
Storage::makeDirectory($directoryPath);
//Perform storing
}
但我有一个错误,“mkdir():无效参数”。
我能知道为什么会这样吗
在我的研究之后,有人说文件夹名称应该基于id+令牌,这样入侵者就不能基于id搜索图像,有可能实现吗?我也有同样的问题,但当我使用
文件
而不是存储
时,它就工作了
$id=1;
$directoryPath=public_path('pages/'。$id);
//检查目录是否存在
如果(!File::isDirectory($directoryPath)){
//创建目录,因为它不存在
File::makeDirectory($directoryPath);
}
//执行文件的存储
希望这能奏效 使用
Storage
facade时,默认使用local
磁盘,该磁盘配置为使用Storage\u path()
因此,如果您想在public
目录中创建目录,请使用File::makeDirectory
,它只是带有附加选项的mkdir()
的简单包装器,或者直接使用mkdir()
:
File::makeDirectory($directoryPath);
mkdir($directoryPath);
在else条件下,只需使用File::makeDirectory instande of Storage::makeDirectory的可能重复项
For basic Laravel file system the syntax is :
At very top under namespace write:
use File;
Then in your method use:
$id=1;
$directoryPath=public_path('pages/' . $id);
if(File::isDirectory($directoryPath)){
//Perform storing
} else {
File::makeDirectory($directoryPath, 0777, true, true);
//Perform storing
}