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.
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);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With