Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing PHP how do I use recursive programming and mkdir() to create this hierarchy?
    primarykey
    data
    text
    <p>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!</p> <p>Example 1</p> <pre><code>$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 ... </code></pre> <p>Example 2</p> <pre><code>$partitionMap = '#/#/#'; //An example of three levels deep myfolder/a myfolder/a/4 myfolder/a/4/f </code></pre> <p>Example 3</p> <pre><code>$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 </code></pre> <p>The # is a hexadecimal placeholder presenting 0-F as you can see above.</p> <p>So I need something that works like this..</p> <pre><code>$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&lt;$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 ); } } } </code></pre> <p>There's got to be a more elegant way of doing this, Any help would be appreciated!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload