Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6588265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:01:36+00:00 2026-05-25T17:01:36+00:00

I want to create a directory structure based on a partition map, as in

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:01:36+00:00Added an answer on May 25, 2026 at 5:01 pm

    A purer recursive solution

    You already have one, but I ‘ll give another spin on it that looks “purer” to my eyes and also explain how I got there.

    Here’s the code:

    function create_all_dirs($directory, $map) {
        // 1: TERMINATION CONDITION
        if (empty($map)) {
            mkdir($directory, 0755, true);
            return;
        }
    
        // 2: PROCESS PART OF DATA, PREPARE DATA TO PASS ON RECURSION
        $parts = explode('/', $map);
        $partLength = strlen(array_shift($parts));
        $dirCount = pow(16, $partLength);    
        $map = implode('/', $parts);
    
        // 3: RECURSE
        for($i = 0; $i < $dirCount; ++$i) {
            $dir = $directory.DIRECTORY_SEPARATOR
                             .sprintf('%0'.$partLength.'s', dechex($i));
            create_all_dirs($dir, $map);
        }
    }
    

    Explanation

    While I won’t even suggest that there’s one good way of reaching a solution for recursive functions, I can explain an approach that has worked well in practice for me.

    Step 1: Think about what data you will need to pass when recursing.

    In this case, every level of the function will be responsible for creating one level of the directory structure. So it makes sense that if you call

    create_all_dirs('c:/', '#/#')
    

    then at some point the function will make a recursive call that looks like this:

    create_all_dirs('c:/0', '#')
    

    That’s all you need to pin down at this point. You can tell that it makes sense and that after a sequence of such calls the function will have (somehow, we haven’t yet written it) done its job.

    Step 2: Termination condition

    With this knowledge, pin down your function’s termination condition. In our case, given the above a reasonable termination condition would be “when the second parameter is the empty string”. So what would the function do if called with a directory and an empty “map” string?

    function create_all_dirs($directory, $map) {
        if(empty($map)) {
            mkdir($directory, 0755, true);
            return;
        }
    
        // ???
    }
    

    Obviously it should create the directory. I ‘m calling mkdir in such a way that it will recursively create all directories on the path if they don’t already exist because the final solution needs it to work, but that’s not necessary. You already know how to do it without this convenience.

    Step 3: Prepare the parameters common to all recursive calls

    OK, so what do we need to make the recursive call? You already know that at each level we need to pop part of $map string and call the function recursively a number of times. We also need to feed it a new, deeper directory string and a new, shorter map string (all of this you could tell from the conclusion of step 1, even if you had not already created a solution).

    So we have:

    // cut off part of $map; we also need the length of the part we cut
    // off, to know how many times we need to recursively call at this level
    $parts = explode('/', $map);
    $partLength = strlen(array_shift($parts));
    
    // how many recursive calls?
    $dirCount = pow(16, $partLength);
    
    // this will be the $map parameter for all recursive calls
    $map = implode('/', $parts);
    

    Step 4: Call the function recursively

    At this point I don’t think there is anything more to explain. We calculate the value of $directory, which is different for each recursive call, and make it:

    for($i = 0; $i < $dirCount; ++$i) {
        $dir = $directory.DIRECTORY_SEPARATOR
                         .sprintf('%0'.$partLength.'s', dechex($i));
        create_all_dirs($dir, $map);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create an empty directory structure bar2 from a nonempty directory tree
How can i create a directory structure with an unknown depth? I want to
I want to create a directory structure like the following. How can I get
I want to create a tar archive with a hierarchical directory structure from Python,
I'm using ActiveState Perl on Windows Server 2003. I want to create a directory
I want to create a business directory project. I want to start two businesses
I want to programmatically create a directory on the server using ASP.NET. I have
I want to create a file in the current directory (where the executable is
I want to create a series of files under log directory which every file
Is it possible to change current directory from a script? I want to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.