note 26721 deleted from function.readdir by tularis

From: Date: Sun, 13 May 2007 19:07:25 +0000
Subject: note 26721 deleted from function.readdir by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-126559@lists.php.net to get a copy of this message
Note Submitter: rolandfx_SPAMMENOT_ at bellsouth dot net ---- 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); ?>

« previous php.notes (#126559) next »