note 26721 added to function.readdir
| From: | rolandfx_SPAMMENOT_ at bellsouth dot net | Date: | Sun, 10 Nov 2002 19:04:03 +0000 |
| Subject: | note 26721 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-39268@lists.php.net to get a copy of this message | ||
While I won't make any claims as to its efficiency, this modified version of
me@infinito.dk's script above does do several things the previous one doesn't. It will
display jpegs using the .jpeg extension, and will not show double-extended files (foo.jpg.bar), nor
will it show non-image files with an extension in their name, like mygif.html.
<?
function CheckExt($filename, $ext) {
$passed = FALSE;
$testExt = "\.".$ext."$";
if (eregi($testExt, $filename)) {
$passed = TRUE;
}
return $passed;
}
//Define an array of common extensions.
$exts = array("gif","jpg$|\\.jpeg","png","bmp");
echo "<b>Images in this folder:</b>";
$dir = opendir(".");
$files = readdir($dir);
while (false !== ($files = readdir($dir))) {
foreach ($exts as $value) {
if (CheckExt($files, $value)) {
echo "<a href=\"$files\">$files</a>\n";
$count++; //Keep track of the total number of files.
break; //No need to keep looping if we've got a match.
}
}
}
echo $count." image(s) found in this directory.\n";
echo "<a
href=\"".$_SERVER["PHP_SELF"]."\">Refresh</a>\n";
//Be a good script and clean up after yourself...
closedir($dir);
?>
--
http://www.php.net/manual/en/function.readdir.php
http://master.php.net/manage/user-notes.php?action=edit+26721
http://master.php.net/manage/user-notes.php?action=delete+26721
http://master.php.net/manage/user-notes.php?action=reject+26721