Page 1 of 1
Help understanding recursive version of glob <Solved>
Posted: Thu Jan 24, 2008 1:56 pm
by Elliott
I found this function on php.net in the comments for glob but I'm having trouble understanding it. My server has PHP5 by the way.
Code: Select all
function getRecursiveFolderList($curDir,$currentA=false)
{
$dirs = glob($curDir . '/*', GLOB_ONLYDIR);
$cur = 0;
foreach($dirs as $dir)
{
$currentA[$cur]['path'] = $dir;
$currentA[$cur] = $this->getRecursiveFolderList($dir,$currentA[$cur]);
++$cur;
}
return $currentA;
}
This is how I attempted to use it:
Code: Select all
$test = array();
echo getRecursiveFolderList("images",$test);
I got this error on line 9 of the above:
"Fatal error: Using $this when not in object context"
How should that line be written? Or am I missing something more basic?
Thanks
-Elliott
Re: Help understanding recursive version of glob
Posted: Thu Jan 24, 2008 2:09 pm
by Kieran Huggins
That function is generally just <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span>'d. I'm actually shocked that it's still in the manual after all these months!
It was obviously part of a class somewhere, but it also looks like it was hacked together by someone on drugs, while tired.
Instead, look a few comments down for
http://php.net/manual/en/function.glob.php#73145
Re: Help understanding recursive version of glob
Posted: Thu Jan 24, 2008 3:35 pm
by Elliott
Thanks, that worked. Although, not the way originally I thought it would.
I expected a recursive directory listing of something like:
/dir01/subdir01/sub2dir01...etc
to make a multidimensional array where:
array[0] = dir01
array[0][0] = subdir01
array[0][0][0] = sub2dir01
and so on...
Since that is not the case, how can I do such a thing? Is glob() the function I should be using?
Thanks
-Elliott
Re: Help understanding recursive version of glob
Posted: Thu Jan 24, 2008 3:48 pm
by VladSun
Elliott wrote:Thanks, that worked. Although, not the way originally I thought it would.
I expected a recursive directory listing of something like:
/dir01/subdir01/sub2dir01...etc
to make a multidimensional array where:
array[0] = dir01
array[0][0] = subdir01
array[0][0][0] = sub2dir01
It doesn't make any sense to iterate through hierarchical data in order to get hierarchical data again ... Why would you want this? What are you trying to do?
Re: Help understanding recursive version of glob
Posted: Thu Jan 24, 2008 4:30 pm
by Elliott
Okay,
I'm writing a program for a webcomic I do with some friends. I thought, to keep everything in order, I'd create a directory structure like comics/year/month/day/comic.jpg
That way, I can generate thumbnails with gd or something and keep them in the same dir. And, as a bonus, I can print out links for the previous or next comic in chronological order from a multidimensional array of the folders.
Is there a better way to do this?
Thanks
-Elliott
Re: Help understanding recursive version of glob
Posted: Thu Jan 24, 2008 6:42 pm
by Elliott
Wow. So a bit of thought, and I think I see why I don't need that array (Sorry, and thanks for saying so). I guess it would be a bit of a burden on the server to make it generate that array every time the page loads. (That is how PHP works right?)
I think instead I'll just count up or down from todays date and check for existing dir's as needed. Is that a better plan, or is that still too intensive to carry out every time the page loads?
Thanks,
Elliott
Re: Help understanding recursive version of glob <Solved>
Posted: Fri Jan 25, 2008 1:34 am
by Kieran Huggins
You should really give a database a thought - it would be waaaay easier, and much better performance-wise.