2

I'm trying to create a folder tree from an array, taken from a string.

$folders = str_split(564);

564 can actually be any number. The goal is to create a folder structure like /5/6/4

I've managed to create all folders in a single location, using code inspired from another thread -

for ($i=0;$i<count($folders);$i++) {
    for ($j=0;$j<count($folders[$i]);$j++) {
        $path .= $folders[$i][$j] . "/";
        mkdir("$path");
    }
    unset($path);
}

but this way I get all folders in the same containing path. Furthermore, how can I create these folders in a specific location on disk? Not that familiar with advanced php, sorry :(

Thank you.

1
  • Figured out in the meantime, thanks to a smarter guy than me :)$folders = str_split(564); mkdir(implode('/',$folders),0777,true); Commented May 17, 2014 at 20:54

4 Answers 4

1

This is pretty simple.

Do a for each loop through the folder array and create a string which appends on each loop the next sub-folder:

<?php

$folders = str_split(564);

$pathToCreateFolder = '';
foreach($folders as $folder) {
   $pathToCreateFolder .= DIRECTORY_SEPARATOR . $folder;
   mkdir($folder);
}

You may also add the base path, where the folders should be created to initial $pathToCreateFolder.

Here you'll find a demo: http://codepad.org/aUerytTd

Or you do it as Michael mentioned in comments, with just one line:

mkdir(implode(DIRECTORY_SEPARATOR, $folders), 0777, TRUE);

The TRUE flag allows mkdir to create folders recursivley. And the implode put the directory parts together like 5/6/4. The DIRECTORY_SEPARATOR is a PHP constant for the slash (/) on unix machines or backslash (\) on windows.

Sign up to request clarification or add additional context in comments.

2 Comments

It's even simpler if you use the $recursive parameter to mkdir(), eliminating the loop entirely. us3.php.net/manual/en/function.mkdir.php
as in $path = implode(DIRECTORY_SEPARATOR, $folders); mkdir($path, 0777, true);
0

Why not just do:

<?php
$directories = str_split(564);

$path = implode(DIRECTORY_SEPARATOR, $directories);

mkdir($path, 0777, true);

Comments

0

Don't know what you're really trying to do, but here are some hints.

There are recursive mkdir:

if(!file_exists($dir)) // check if directory is not created
{
   @mkdir($dir, 0755, true); // create it recursively
}

Path you want can be made in two function calls and prefixed by some start path:

$path = 'some/path/to/cache';

$cache_node_id = 4515;

$path = $path.'/'.join('/', str_split($cache_node_id));

Resulting path can be used to create folder with the code above

So here we come to a pair of functions/methods

function getPath($node_id, $path = 'default_path')
{
  return $path.'/'.join('/', str_split($node_id))
}

function createPath($node_id, $path = 'default_path');
{

   $path = getPath($node_id, $path);

   if(!file_exists($path)) // check if directory is not created
   {
     @mkdir($path, 0755, true); // create it recursively
   }
}

With these you can easily create such folders everywhere you desire and get them by your number.

2 Comments

Oh god, stop using @ to suppress errors. Handle the errors.
@HoshSadiq set_error_handler code for permission errors is not required to answer the question thus it is just an example. I suppose, reader should decide for himself what to do with it.
0

As mentioned earlier, the solution I got from a friend was

$folders = str_split(564); 
mkdir(implode('/',$folders),0777,true);

Also, to add a location defined in a variable, I used

$folders = str_split($idimg);
mkdir($path_defined_earlier. implode('/',$folders),0777,true);

So thanks for all the answers, seems like this was the correct way to handle this. Now the issue is that I need to the created path, so how can I store it in a variable? Sorry if this breaches any rules, if I need to create a new thread I'll do it...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.