TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   recursive glob ? (http://www.talkphp.com/general/5242-recursive-glob.html)

Peuplarchie 01-31-2010 03:23 AM

recursive glob ?
 
Good day to you all,
is there a way that I can use glob recursive ly in the following code ?

PHP Code:


<?PHP
foreach(glob('Photos/*'GLOB_ONLYDIR) as $dir) {
  echo 
'<b>'.$dir.'</b><br>';
}  
?>


Thanks!

ETbyrne 01-31-2010 03:54 AM

If you look down in the comments on the docs page, you will find many solutions. One such being this:

PHP Code:

$dir_iterator = new RecursiveDirectoryIterator("/path");
$iterator = new RecursiveIteratorIterator($dir_iteratorRecursiveIteratorIterator::SELF_FIRST);
// could use CHILD_FIRST if you so wish

foreach ($iterator as $file) {
    echo 
$file"\n";


Of coarse, that would return both files and folders. Some other options are also available such as running glob in a loop of some sort.

ETbyrne 01-31-2010 04:19 AM

OK, here's a sweet little function I made that does exactly what you need:

PHP Code:

function glob_recursive($dir)
{
    static 
$g = array();
    
    foreach(
glob($dir,GLOB_ONLYDIR) as $i=>$k)
    {
        
$g[] = $k;
        
glob_recursive($k.'/*');
    }
    
    return 
$g;


And use like so:

PHP Code:

print_r(glob_recursive('dingo/*')); 

The only problem is, you can't use it twice. I'll let you figure out how to fix that though. :-)

Peuplarchie 01-31-2010 04:37 AM

Yes ok,
How can I but one on each line ?

Can I also not the parentasis and the "array" word and his number ?

How can I display the folder that has folder in it as bold.
And the folders that as files in it, as links ?

ETbyrne 02-02-2010 10:46 PM

It sounds like you don't know much about arrays.. You may want to read this:

http://us.php.net/manual/en/language.types.array.php

The function I gave you returns an array which you can loop through and format as you wish. The rest is up to you man.

scottchu 09-10-2012 06:31 AM

Your code skips all files
 
Quote:

Originally Posted by ETbyrne (Post 29853)
OK, here's a sweet little function I made that does exactly what you need:

PHP Code:

function glob_recursive($dir)
{
    static 
$g = array();
    
    foreach(
glob($dir,GLOB_ONLYDIR) as $i=>$k)
    {
        
$g[] = $k;
        
glob_recursive($k.'/*');
    }
    
    return 
$g;


And use like so:

PHP Code:

print_r(glob_recursive('dingo/*')); 

The only problem is, you can't use it twice. I'll let you figure out how to fix that though. :-)

Your code skips all files and will return an empty array if there's no any subfolders.

scottchu 09-10-2012 06:54 AM

Your code skips all files
 
Quote:

Originally Posted by ETbyrne (Post 29853)
OK, here's a sweet little function I made that does exactly what you need:

PHP Code:

function glob_recursive($dir)
{
    static 
$g = array();
    
    foreach(
glob($dir,GLOB_ONLYDIR) as $i=>$k)
    {
        
$g[] = $k;
        
glob_recursive($k.'/*');
    }
    
    return 
$g;


And use like so:

PHP Code:

print_r(glob_recursive('dingo/*')); 

The only problem is, you can't use it twice. I'll let you figure out how to fix that though. :-)

Your code skips all files and will always return an empty array if there's no subfolders initially. A revised codes could be:

PHP Code:

function glob_recursive($dir)
{
    static 
$g = array();
    
    foreach(
glob($dir) as $i=>$k)
    {
        if (
is_dir($k))
            
glob_recursive($k.'/*');
        else
            
$g[] = $k;
    }
    return 
$g;
}

// Show human-readable dump of array contents
echo nl2br(print_r(glob_recursive('dingo/*'),true)); 



All times are GMT. The time now is 10:28 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0