Welcome Guest, Not a member yet? Register   Sign In
Using function directory_map
#1

[eluser]polaris1927[/eluser]
I am using the
Code:
directory_map($path)
function to build an array list of directories and sub-directories below $path.

However, it seems
Code:
directory_map($path)
does not return the path for sub directories.

My question is this: is there a way that I can get the full sub-directory paths when using directory_map?

Thanks
#2

[eluser]Colin Williams[/eluser]
Are you sure. Works just that way for me. Files are strings and folders area keyed arrays, so make sure in your loop your using $key => $value in your loop. If $value is an array, $key is your folder name.
#3

[eluser]polaris1927[/eluser]
Thanks for the reply

Yes, I am able to get the folder and file names, but I want the path to those sub folders. for example

if $path = '/home/test';

--Main folder (array)
--- Sub folder 1 (array)
------- sub folder 1.1 (array)
--- Sub fouler 2 (array)
------- Sub folder 2.1 (array)
------- sub folder 2.2 (array)

Using directory_map returns folders as arrays; I need the actual path of the sub folders

/home/test/Main folder
/home/test/Main folder/Sub folder 1
/home/test/Main folder/sub folder 1/sub folder 1.1
#4

[eluser]Colin Williams[/eluser]
Well then, recurse the array and store each level along the way.
#5

[eluser]polaris1927[/eluser]
What would be the best approach to acieve this, could you post an example code structure to explain what you mean?

Thanks
#6

[eluser]Colin Williams[/eluser]
By what means do you wish to retrieve the structure? Do you want to fetch a string with a certain folders complete structure, or do you wish to fetch an array where each value is a full path to a folder in the map?
#7

[eluser]polaris1927[/eluser]
Where each value is a full path.
#8

[eluser]Colin Williams[/eluser]
I loves me some recursion!

Call this function like you would call directory_map(), or call it with 1) A folder path to prefix your array values and 2) a map already returned by directory_map(). Be sure to load directory helper first, or better yet, put this and directory_map() in your own helper.

Code:
function directory_map_paths($wp = '', $map = array())
{
   static $paths = array();
   if (!is_array($map) or !count($map))
   {
      if ($wp)
      {
         $map = directory_map($wp);
         $wp = '';
      }
      else
      {
         show_error('No map or working path given to remap.');
      }
   }
   if (count($map))
   {
      foreach ($map as $k => $v)
      {
         if (is_array($v))
         {
            directory_map_paths($wp . ($wp ? '/' : '') . $k, $v); // Recurse
         }
         else
         {
            $paths[] = $wp . ($wp ? '/' : '') . $v;
         }
      }
   }
   return $paths;
}
#9

[eluser]polaris1927[/eluser]
Thanks!
#10

[eluser]Colin Williams[/eluser]
Shit yeah, polaris! You owe me a beer.




Theme © iAndrew 2016 - Forum software by © MyBB