简体   繁体   中英

php mkdir folder tree from array

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.

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.

Why not just do:

<?php
$directories = str_split(564);

$path = implode(DIRECTORY_SEPARATOR, $directories);

mkdir($path, 0777, true);

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.

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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM