Help understanding recursive version of glob <Solved>

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Elliott
Forum Newbie
Posts: 7
Joined: Thu Jan 24, 2008 11:36 am
Location: Indiana, United States

Help understanding recursive version of glob <Solved>

Post 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
Last edited by Elliott on Thu Jan 24, 2008 9:00 pm, edited 1 time in total.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Help understanding recursive version of glob

Post by Kieran Huggins »

That function is generally just <span style='color:blue' title='I&#39;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
Elliott
Forum Newbie
Posts: 7
Joined: Thu Jan 24, 2008 11:36 am
Location: Indiana, United States

Re: Help understanding recursive version of glob

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Help understanding recursive version of glob

Post 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?
There are 10 types of people in this world, those who understand binary and those who don't
Elliott
Forum Newbie
Posts: 7
Joined: Thu Jan 24, 2008 11:36 am
Location: Indiana, United States

Re: Help understanding recursive version of glob

Post 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
Elliott
Forum Newbie
Posts: 7
Joined: Thu Jan 24, 2008 11:36 am
Location: Indiana, United States

Re: Help understanding recursive version of glob

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Re: Help understanding recursive version of glob <Solved>

Post by Kieran Huggins »

You should really give a database a thought - it would be waaaay easier, and much better performance-wise.
Post Reply