Glob examples

Shows how to use the glob function to read directory listings as with "opendir" - just easier :-)

$dir = './';
 
foreach(glob($dir.'*.txt') as $file) {
    print $file . "\n";
}
 
/* returns: 
 
./dummy.txt
./foo.txt
./ideas.txt
./robots.txt
./scite.txt
 
*/
 
 
/*
** other examples:
*/
 
// also possible:
$files = glob('*.*');
sort($files);
 
 
// This shows how to use the GLOB_BRACE flag:
$images = glob("images/{*.jpg,*.gif,*.png}", GLOB_BRACE);
print_r($images);
 
 
/* Valid flags:
 
GLOB_MARK
GLOB_NOSORT
GLOB_NOCHECK
GLOB_NOESCAPE
GLOB_BRACE
GLOB_ONLYDIR
GLOB_ERR
 
see PHP.net manual for more info
*/
Snippet Details




Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

MD May 09, 2011 at 14:47
My glob() Function Is Not Accepting Path
In Array.

I Want Set Path Dynamically From Database But It's Not Working.
My Coad As Follwing I Need Help.

$slIm1=mysql_query("select ImgPath from subcategory_2
where SubCategory='".$id."'");


//Get Path In Array.
while($Path=mysql_fetch_array($slIm1))
{
$files=glob($Path['ImgPath']."*.*"
.'.{jpg,jpeg,png,gif},GLOB_BRACE');
for($i=0;$i<count($files);$i++)
{
$num=$files[$i];
echo "<img src='".$num."'>";
}

Thanks.
Jonas January 20, 2011 at 14:28
Thanks LED!

When I can find some time beside my job I'll post some more snippets in 2010.
LED January 18, 2011 at 06:01
No replies for 2010 so thought I'd be the 1st for 2011...Thanks for the past J.J. and hope to see more :)
bassman February 19, 2009 at 23:21
Very good examples, better then in the manual.
Thank you Jonas
vijay December 01, 2008 at 11:59
very super explain this function