note 76377 deleted from function.readdir by danbrown
| From: | danbrown@php.net | Date: | Thu, 25 Feb 2010 20:29:14 +0000 |
| Subject: | note 76377 deleted from function.readdir by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-166365@lists.php.net to get a copy of this message | ||
Note Submitter: moehbass at gmail dot com
----
Responding to:
johan dot mickelin at gmail dot com
31-May-2007 07:52
-------------------------------------------------------
If you want to list only a certain filetype, this case only jpg and gif files in an image directory
$dir = opendir ("../images");
while (false !== ($file = readdir($dir))) {
if (strpos($file, '.gif',1)||strpos($file, '.jpg',1) ) {
echo "$file <br />";
}
}
-----------------------------------------------------------------
This function would also echo files that have .gif or .jpg in their names such as myFile.gif.php (I
don't know why I'd name a file like that, but I am just making a point, that's all
folks!)
Perhaps a more exact way is to do the following:
...
while (false !== ($file = readdir($dir))) {
$lenOfFileName = strlen($file);
$extOffsetPos = $lenOfFileName - 5;
if (strpos($file, '.gif', $extOffsetPos) ||
strpos($file, '.jpg',$extOffsetPos) ) {
echo "$file <br />";
}
...
If your extensions are more than three letters, then increase the 5 to 6 (e.g. aspx) to 7 (e.g.
php51) or whatever.