Php Laravel创建存储文件夹子文件夹

Php Laravel创建存储文件夹子文件夹,php,laravel,laravel-5.5,Php,Laravel,Laravel 5.5,在我的Laravel存储文件夹中,我有一些子文件夹,例如users或admins。是否有类似于存储文件夹的播种机?因为我需要确保在将文件保存到它时,用户存在(不检查文件夹是否确实存在),而且我不想手动创建这些文件夹,从Git拉出并部署。我相信我可能有一段有趣的代码供您使用: /** * Generate directory structure * @param $basePath, path where all directories will be created

在我的Laravel存储文件夹中,我有一些子文件夹,例如
users
admins
。是否有类似于存储文件夹的播种机?因为我需要确保在将文件保存到它时,
用户
存在(不检查文件夹是否确实存在),而且我不想手动创建这些文件夹,从Git拉出并部署。

我相信我可能有一段有趣的代码供您使用:

    /**
     * Generate directory structure
     * @param $basePath, path where all directories will be created in
     * @param $relativePath, recursive array in structure of directories
     * @param $permission, permission code to apply to folders
     */
    function generateDirectories($basePath, $relativePath, $permission)
    {
        //If array, unfold it
        if(is_array($relativePath))
        {
            foreach($relativePath as $key => $path)
            {
                //If key is not numeric, add it to foldername, else empty
                $folderName = is_numeric($key) ? '' : '\\' . $key;
                $this->generateDirectories($basePath . $folderName, $path, $permission);
            }
        }
        //Else handle it
        else
        {
            try
            {
                $this->fileSystem->makeDirectory($basePath . '\\' . $relativePath, $permission, true);
                $this->log('Successfully made directory: ' . $basePath . '\\' . $relativePath, 0);
            }
            catch(\Exception $ex)
            {
                //Do your logging or whatever
            }
        }
    }
基本上为其提供如下数组:

[
    $moduleName => [
      'src' => [
        'Commands',
        'Contracts',
        'Controllers',
        'Models',
        'Providers',
        'DataTables',
        'Routes',
        'Migrations',
        'Resources' => [
            'Views',
            'Lang'
        ],
        'Assets' => [
            'js',
            'css'
        ],
    ],
]
它开始构建你的文件夹结构


希望有帮助:)

您是如何在这些子文件夹中存储文件的?因为默认情况下,
文件系统
实际上会在这些文件夹不存在时自动创建它们。我只是使用
$request->file('file')->store('…')
。如果这已经奏效了,那么,
本地
文件系统驱动程序会在找不到目录时递归创建文件夹。第三个参数
true
表示递归创建文件夹。