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
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack (10) Thread Tools Display Modes
Old 09-07-2007, 12:21 PM   10 links from elsewhere to this Post. Click to view. #1 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 1,542
Thanks: 72
Wildhoney is on a distinguished road
Traverse Directories the Easy Way with Glob() !

There is a powerful yet somewhat unheard of function in PHP titled glob(). Many beginners and intermediates alike struggle with the traversing of directories. Acquiring the directories contents and returning them in a readable format. If only they know how so very easy this task was!

Glob was introduced into the PHP function lineup around the time PHP version 4 was released. It's not a new function in the slightest, but like checkdnsrr() it is a fairly undiscovered function for many.

The catchy misnomer that has long been used as a Unix library function, has a similar syntax reminiscent to regular expressions without the expressive power. It allows you to bring back filenames, and also directories, using a straightforward notation.

The Syntax

PHP Code:
foreach(glob('myDirectory/*.php') as $szFilename)
{
    echo 
'Filename: ' $szFilename '<br />';

Glob supports a maximum of 2 arguments, where the 2nd argument is optional. This means you can either omit it entirely, or fill its second argument with NULL.

The tiny bit of code above shows the immense power of glob when it comes to harvesting the names of files, and optionally, directories too, from a specified directory - in our case, myDirectory. It will return every file that is contained within myDirectory and that ends with .php. So for instance, index.php would be brought back, but default.asp would not because it ends in .asp.

As you can see, you can place glob straight into a foreach loop where the filename for each single file that matches the notation will be stored as a string in the user variable, $szFilename. This can be echoed straight out or used in conjunction with other functions, for example, filesize(). The string that is returned by the glob function will always be relative to your script based on the notation you are passing to glob.

Optional Flags

You may use the 2nd argument to make glob perform in many different and equally wonderful ways. To give a working example of this, you can use GLOB_BRACE to bring back 2 types of files like I have done in the following example:

PHP Code:
$aFiles glob('{myDirectory/*.jpg,myDirectory/*.gif}'GLOB_BRACE); 
This brings back all the files in myDirectory ending with JPG and all those in myDirectory ending in GIF. I have informed glob that I am using the braces to group the patterns via GLOB_BRACE.

PHP.net defines the following constants to be used as the optional 2nd argument:
  • GLOB_MARK - Adds a slash to each item returned
  • GLOB_NOSORT - Return files as they appear in the directory (no sorting)
  • GLOB_NOCHECK - Return the search pattern if no files matching it were found
  • GLOB_NOESCAPE - Backslashes do not quote metacharacters
  • GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c'
  • GLOB_ONLYDIR - Return only directory entries which match the pattern
  • GLOB_ERR - Stop on read errors (like unreadable directories), by default errors are ignored.

Conclusion

...And that about brings us to a pleasant conclusion on glob! The next time you're perplexed on how to obtain all the files from a directory, or the directories themselves using GLOB_ONLYDIR. Glob is your man! Or woman. Or how about a nice generic "it"?

One thing I am sure about is that glob has too much of a conspicuous name to be placed in a corner and forgotten about. It DEMANDS attention! Much like your girlfriend at half 10 on a Saturday night when you're trying to watch Match Of The Day.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following 2 Users Say Thank You to Wildhoney For This Useful Post:
ETbyrne (09-22-2008), Runar (11-10-2008)
Old 09-07-2007, 01:36 PM   #2 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 700
Thanks: 2
Salathe is on a distinguished road
Default

To put glob to a slightly more practical use, in the past I've "auto-loaded" plugins as part of a framework -- if I placed a plugin file into the plugins directory then it was automatically included and available to use.

PHP Code:
// Example plugins file/folder structure:
//    plugins/
//        assets.php
//        typography.php
//
// This would include assets.php and typography.php
// along with any other plugin files in that folder.
foreach(glob('plugins/*.php') as $szPlugin)
{
    include_once 
$szPlugin;


On a more technical note, because not everyone reads the manual, it is possible to 'or' the flags for second argument if you wish to apply more than one of the available flags.
PHP Code:
// Would return files in the images folder ending with .jpg, .gif or .png
$aFiles glob('images/{*.jpg,*.gif,*.png}'GLOB_BRACE GLOB_NOSORT); 
Salathe is offline  
Reply With Quote
Old 09-07-2007, 03:28 PM   #3 (permalink)
Super Moderator
Advanced Programmer 
 
bluesaga's Avatar
 
Join Date: Sep 2007
Posts: 164
Thanks: 0
bluesaga is on a distinguished road
Default

Very handy, i normally just use dir() and run my own preg functions which i presume glob does, but still an extremely powerful function by the looks of things. I wonder why i haven't come across that before!
bluesaga is offline  
Reply With Quote
Old 09-22-2008, 05:57 PM   #4 (permalink)
The Contributor
 
Evulness's Avatar
 
Join Date: Apr 2008
Location: Tampa, FL
Posts: 62
Thanks: 6
Evulness is on a distinguished road
Default

Oh sweet. I was just starting to look up dir() functions for an image inclusion script for my portfolio...

I love you guys. I always seem to find what i need references for here.

Thank you
__________________
"Knowledge is power. Abuse it."~Evulness
My portfolio: www.evularts.com
Send a message via AIM to Evulness
Evulness is offline  
Reply With Quote
Old 09-24-2008, 12:35 AM   #5 (permalink)
The Acquainted
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 177
Thanks: 34
ETbyrne is on a distinguished road
Default

I just realized that this topic has 43 THOUSAND views! O_o
__________________
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
Reply


LinkBacks (?)
LinkBack to this Thread: http://www.talkphp.com/absolute-beginners/1037-traverse-directories-easy-way-glob.html
Posted By For Type Date
PHP Traverse Directories the Easy Way with Glob() ! Tutorial This thread Refback 01-11-2008 03:15 PM
StumbleUpon - ninom's web site reviews and blog This thread Refback 01-10-2008 01:41 PM
Traverse Directories the Easy Way wit... - web2announcer This thread Refback 12-28-2007 10:15 AM
Popular pages tagged with This thread Refback 12-27-2007 07:36 AM
Hot Website! - What's HOT? This thread Refback 12-26-2007 10:22 PM
THEWEBLIST.net | what people are clicking on today This thread Refback 12-26-2007 09:52 PM
Technology News from Web 2.0 Sites: Programming, web design, encryption, education This thread Refback 12-26-2007 08:46 PM
popurls® | popular urls to the latest web buzz This thread Refback 12-26-2007 08:28 PM
THEWEBLIST.net | what people are clicking on today This thread Refback 12-26-2007 07:18 PM
PHP File Manipulation Traverse Directories the Easy Way with Glob() ! Tutorial This thread Refback 12-22-2007 11:22 PM

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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


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

 
     

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