note 78710 added to function.readdir
| From: | adrongspATamdealslab dot com at osu1 dot php dot net | Date: | Wed, 24 Oct 2007 16:10:12 +0000 |
| Subject: | note 78710 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131689@lists.php.net to get a copy of this message | ||
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);
}
?>
----
Server IP: 209.41.74.194
Probable Submitter: 12.34.246.36
----
Manual Page -- http://www.php.net/manual/en/function.readdir.php
Edit -- https://master.php.net/note/edit/78710
Del: integrated -- https://master.php.net/note/delete/78710/integrated
Del: useless -- https://master.php.net/note/delete/78710/useless
Del: bad code -- https://master.php.net/note/delete/78710/bad+code
Del: spam -- https://master.php.net/note/delete/78710/spam
Del: non-english -- https://master.php.net/note/delete/78710/non-english
Del: in docs -- https://master.php.net/note/delete/78710/in+docs
Del: other reasons-- https://master.php.net/note/delete/78710
Reject -- https://master.php.net/note/reject/78710
Search -- https://master.php.net/manage/user-notes.php