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

时间:2018-03-18 15:09:05

标签: php laravel laravel-5.5

在我的Laravel存储文件夹中,我有子文件夹,例如usersadmins。是否有类似存储文件夹的播种机?因为我需要确保在保存文件时users存在(不检查文件夹是否实际存在),而且我不想手动从Git和部署中创建这些文件夹。

1 个答案:

答案 0 :(得分:2)

我相信我可能会为您提供一段有趣的代码:

    /**
     * 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'
        ],
    ],
]

它开始构建你的文件夹结构。

希望有所帮助:)