Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正在检查文件夹是否已存在,如果不存在,请在laravel中按id创建新文件夹_Laravel_Laravel 5 - Fatal编程技术网

正在检查文件夹是否已存在,如果不存在,请在laravel中按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命名。例如,如果页面id=1,则此处的文件夹位置应为public/pages/page\u id/image\u

如果文件夹尚不存在,系统将生成并使用其页面id命名

 $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
}