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
 
 
LinkBack (10) Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old 09-07-2007, 11:21 AM   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: 2,280
Thanks: 90
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 5 Users Say Thank You to Wildhoney For This Useful Post:
codefreek (12-02-2008), ETbyrne (09-22-2008), Peuplarchie (01-21-2009), Runar (11-10-2008), zxt3st (12-02-2008)
 


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
Untitled document 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 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


All times are GMT. The time now is 08:10 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