检查文件夹已经存在,如果没有在laravel中通过id创建一个新文件夹
问题描述:
我想将我的图像存储到特定的文件夹,并且该文件夹将按页面ID命名。例如,如果Page id = 1,文件夹位置应该是public/pages/page_id/image_here。检查文件夹已经存在,如果没有在laravel中通过id创建一个新文件夹
如果该文件夹不存在,系统将生成并用其页面标识进行命名。
$id=1;
$directoryPath=public_path('pages/' . $id);
if(File::isDirectory($directoryPath)){
//Perform storing
} else {
Storage::makeDirectory($directoryPath);
//Perform storing
}
但我有错误,“mkdir():无效的参数”。 我可以知道它为什么会发生?
经过我的研究,有人说文件夹名称应该以id + token为基础,所以入侵者不能搜索基于id的图片,有没有可能实现?
答
当您使用Storage
外观时,它将默认使用local
磁盘,该磁盘被配置为与storage_path()
配合使用。
所以,如果你想在public
目录下创建目录,使用File::makeDirectory
这只是简单的包装为mkdir()
有其他选项,或直接使用mkdir()
:在其他条件
File::makeDirectory($directoryPath);
mkdir($directoryPath);
答
我有同样的问题,但是当我用File
而不是Storage
它的作品!
$id=1;
$directoryPath=public_path('pages/' . $id);
if(File::isDirectory($directoryPath)){
//Perform storing
} else {
File::makeDirectory($directoryPath);
//Perform storing
}
希望这个作品!
答
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
}
只是用文件: :makeDirectory存储:: makeDirectory的实例 –
可能重复[在laravel中创建文件夹](http://stackoverflow.com/questions/21869223/create-folder-in-laravel) –