TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 01-31-2010, 03:23 AM   #1 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Application 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!
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 01-31-2010, 03:54 AM   #2 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

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.
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 01-31-2010, 04:19 AM   #3 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

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.
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
The Following User Says Thank You to ETbyrne For This Useful Post:
Peuplarchie (01-31-2010)
Old 01-31-2010, 04:37 AM   #4 (permalink)
The Acquainted
 
Peuplarchie's Avatar
 
Join Date: May 2008
Location: Québec
Posts: 104
Thanks: 10
Peuplarchie is on a distinguished road
Default

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 ?
__________________
That's why we are not alone on earth... let's build !
Peuplarchie is offline  
Reply With Quote
Old 02-02-2010, 10:46 PM   #5 (permalink)
how quixotic are you?
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 445
Thanks: 37
ETbyrne is on a distinguished road
Default

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.
__________________
Dingo Web Systems > http://www.dingocode.com
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Old 09-10-2012, 06:31 AM   #6 (permalink)
The Visitor
 
Join Date: Sep 2012
Posts: 2
Thanks: 0
scottchu is on a distinguished road
Default Your code skips all files

Quote:
Originally Posted by ETbyrne View Post
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 is offline  
Reply With Quote
Old 09-10-2012, 06:54 AM   #7 (permalink)
The Visitor
 
Join Date: Sep 2012
Posts: 2
Thanks: 0
scottchu is on a distinguished road
Default Your code skips all files

Quote:
Originally Posted by ETbyrne View Post
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)); 
scottchu is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursive arrays maZtah General 2 11-19-2009 08:13 AM
Glob recursion not working right benton General 2 09-26-2009 03:06 PM
Fix recursive function on a directory listing Peuplarchie General 1 04-13-2009 03:04 AM
Traverse Directories the Easy Way with Glob() ! Wildhoney Absolute Beginners 6 12-02-2008 11:07 PM


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

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design