Question

So I'm guessing you read the title and thought that's easy mkdir() and copy() but what I'm trying to do is not as simple and I couldn't think of a better title so let me explain further...

what I want to do is create a folder then create a 2nd, 3rd, 4th and put each folder made inside the last so I would end up with a folder path like: 1 > 2 > 3 > 4 > 5

I currently have this snippet which creates 5 folders:

$x=1;
while($x<=5) {
   mkdir($x);
 $x++;
} 

but I'm stuck on the moving each to its new home the previous folder.

Note: I know I could just right click desktop create 5 new folders and do it that way but I would like to know how it could be done with php :)

Was it helpful?

Solution

If your PHP version is 5.0.0 or above, you may use the recursive parameter of mkdir:

$path = "folder 1/folder 2/folder 3/folder 4/folder 5/";
mkdir($path, 0777, true);

UPDATE: You may use a for loop to avoid writing the word "folder" five times:

$path = "";
for($i = 1; $i <= 5; $i++) $path .= "folder $i/";
mkdir($path, 0777, true);

OTHER TIPS

I haven't checkd this works but the idea is to build up your path as you go:

$path = '/somepath';
$x = 1;
while($x<=5) {
  $path .= $x . '/'
  mkdir($path);
  $x++
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top