在Laravel中递归创建文件夹

时间:2014-06-21 17:27:56

标签: php laravel-4

我已经在一个类中创建了这个函数,虽然它有效,但我认为它很麻烦并且可以很容易地简化,我可以使用什么技术来简化这个?虽然我可以使用foreach,但每次它都会在Cache文件夹中创建新目录,而不是在父目录中创建目录。

示例:$directory = 'page/subpage/homepage'

public static function add_directory( $directory, $cache_path ) {


    $all_directories = explode( '/', $directory );
    $total_directories = count( $all_directories );


    if( $total_directories == 1 ) {

        if( ! File::exists( $cache_path.'/'.$all_directories[0] ) ) {
            File::makeDirectory( $cache_path.'/'.$all_directories[0] );
        }

    }


    else if( $total_directories == 2 ) {

        if( ! File::exists( $cache_path.'/'.$all_directories[0] ) ) {
            File::makeDirectory( $cache_path.'/'.$all_directories[0] );
        }

        if( ! File::exists( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1] ) ) {
            File::makeDirectory( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1] );
        }

    }


    else if( $total_directories == 3 ) {

        if( ! File::exists( $cache_path.'/'.$all_directories[0] ) ) {
            File::makeDirectory( $cache_path.'/'.$all_directories[0] );
        }

        if( ! File::exists( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1] ) ) {
            File::makeDirectory( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1] );
        }

        if( ! File::exists( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1].'/'.$all_directories[2] ) ) {
            File::makeDirectory( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1].'/'.$all_directories[2] );
        }

    }


    else if( $total_directories == 4 ) {

        if( ! File::exists( $cache_path.'/'.$all_directories[0] ) ) {
            File::makeDirectory( $cache_path.'/'.$all_directories[0] );
        }

        if( ! File::exists( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1] ) ) {
            File::makeDirectory( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1] );
        }

        if( ! File::exists( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1].'/'.$all_directories[2] ) ) {
            File::makeDirectory( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1].'/'.$all_directories[2] );
        }

        if( ! File::exists( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1].'/'.$all_directories[2].'/'.$all_directories[3] ) ) {
            File::makeDirectory( $cache_path.'/'.$all_directories[0].'/'.$all_directories[1].'/'.$all_directories[2].'/'.$all_directories[3] );
        }

    }


    else {}


}

感谢。

2 个答案:

答案 0 :(得分:7)

您可以通过将{true}设置为File::makeDirectory()的第三个参数来指定是否要递归创建目录,这样就可以用以下内容替换整个代码:

public static function add_directory( $directory, $cache_path ) {

    if (!File::exists( $cache_path.'/'.$directory)) {
        File::makeDirectory($cache_path . '/' . $directory, 0755, true);
    }
}

参考:看起来没有记录,但它在Laravel的source code for filesystem/Filesystem.php中:

public function makeDirectory($path, $mode = 0755, $recursive = false, $force = false)
{
    if ($force)
    {
        return @mkdir($path, $mode, $recursive);
    }
    else
    {
        return mkdir($path, $mode, $recursive);
    }
}

答案 1 :(得分:0)

从查看你的代码 - 我不明白为什么这不起作用?

public static function add_directory( $directory, $cache_path ) {

    if( ! File::exists( $cache_path.'/'.$directory ) ) {
            File::makeDirectory( $cache_path.'/'.$directory );
    }
}

似乎不需要'爆炸'数组然后重建它?这将构建所有子目录,我相信......