note 78710 deleted from function.readdir by felipe
| From: | felipe@php.net | Date: | Sun, 16 Dec 2007 11:43:48 +0000 |
| Subject: | note 78710 deleted from function.readdir by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133672@lists.php.net to get a copy of this message | ||
Note Submitter: adrong spATam dealslab.com
Reason: 1
----
Passing $arFiles by reference, the getFiles function will return all jpeg, gif, and png images in a
given directory and its subdirectories in a two dimensional array.
<?php
$startPath='.';
getFiles($startPath, &$arFiles);
sort($arFiles); //produces desired result in PHP v4.4.4; other versions may not.
//$arFiles[INDEX][0] the full directory in which the file is found
//$arFiles[INDEX][1] the filename.
//path and filename = $arFiles[INDEX][0].'/'.$arFiles[INDEX][1]
foreach($arFiles as $val)
//the line below will display the picture.
echo '<img src="'.str_replace('.', '',
$val[0]).'/'.urlencode($val[1]).'"><br>';
//the commented line below will show the path to the picture.
//echo $val[0].'/'.urlencode($val[1]).'<br>';
//I use another PHP script to resize pictures to a common size on the fly,
//then use a more complicated version of this script to place resized images into an HTML table
//echo '<img src=resize.php?pic="'.str_replace('.', '',
$val[0]).'/'.urlencode($val[1]).'"><br>';
function getFiles($dir, $arFiles)
{
$imgFiles=opendir($dir);
while($aFile=readdir($imgFiles) )
if(is_dir($dir.'/'.$aFile) && ereg('[^\.]', $aFile))
getFiles($dir.'/'.$aFile, &$arFiles);
else if(eregi('.*\.jpg|.*\.jpeg|.*\.gif|.*\.png', $aFile))
$arFiles[]=array($dir, $aFile);
}
?>