Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create folders recursively in Laravel

Tags:

php

laravel-4

I have created this function within a Class, And though it works, I think Its messy and can be simplified easily, What technique can i use to simplify this? Though i can use foreach, Every time it will make new directory in the Cache folder instead of creating directory within parent directory.

Example : $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 {}


}

Thanks.

like image 388
Dhruv Kumar Avatar asked Jun 21 '14 17:06

Dhruv Kumar


1 Answers

You can specify whether you want to create directories recursively by setting true to your File::makeDirectory()'s third argument, so you can replace your entire code with something like this:

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

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

Reference: Looks like it is not documented but it is in Laravel's 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);
    }
}
like image 76
Unnawut Avatar answered Oct 03 '22 16:10

Unnawut