I want to create a directory structure based on a partition map, as in these examples. I've tried various approaches with failure. Recursive programming makes my brain hurt!
Example 1
$partitionMap = '#/#'; //An example of two levels deep
myfolder/0
myfolder/1
myfolder/2
...
myfolder/f
myfolder/f/0
myfolder/f/1
myfolder/f/2
myfolder/f/3
...
Example 2
$partitionMap = '#/#/#'; //An example of three levels deep
myfolder/a
myfolder/a/4
myfolder/a/4/f
Example 3
$partitionMap = '#/##/###'; //An example of three levels deep
myfolder/0
myfolder/1
myfolder/2
...
myfolder/a/00
myfolder/a/01
myfolder/e/02
myfolder/e/03
...
myfolder/e/03/af0
myfolder/e/03/af1
myfolder/e/03/af2
myfolder/e/03/af3
The # is a hexadecimal placeholder presenting 0-F as you can see above.
So I need something that works like this..
$partitionMap = '#/##';
makeFoldersRecursive( './myfolder', $partitionMap );
function makeFoldersRecursive( $path, $pmap ){
$pmapArr = explode( '/', $pmap );
$numNibbles = strlen( $pmapArr[0] );
//Get hex folder count but in dec, # = 16, ## = 256, ### = 4096
$folder_count_this_level = pow( 16, $numNibbles );
for($i=0; $i<$count; $i++ ) {
//Get left padded hex folder name from $i
$folder = sprintf("%0" . $numNibbles . "s", dechex($i));
mkdir( $path . DIRECTORY_SEPARATOR . $folder , 0755 );
if( array_key_exists( 1, $pmapArr ) ) {
$passMap = $pmapArr;
array_shift( $pmapArr );
makeFoldersRecursive( $path . DIRECTORY_SEPARATOR . $folder, $passMap );
}
}
}
There's got to be a more elegant way of doing this, Any help would be appreciated!
mkdir()takes a recursive param right? php.net/manual/en/function.mkdir.phpmkdir('/path/to/create', 0755, true);