note 76377 added to function.readdir
| From: | moehbass at gmail dot com | Date: | Thu, 12 Jul 2007 15:27:40 +0000 |
| Subject: | note 76377 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-128415@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 69.147.83.197
Probable Submitter: 196.205.206.106
----
Manual Page -- http://www.php.net/manual/en/function.readdir.php
Edit -- https://master.php.net/note/edit/76377
Del: integrated -- https://master.php.net/note/delete/76377/integrated
Del: useless -- https://master.php.net/note/delete/76377/useless
Del: bad code -- https://master.php.net/note/delete/76377/bad+code
Del: spam -- https://master.php.net/note/delete/76377/spam
Del: non-english -- https://master.php.net/note/delete/76377/non-english
Del: in docs -- https://master.php.net/note/delete/76377/in+docs
Del: other reasons-- https://master.php.net/note/delete/76377
Reject -- https://master.php.net/note/reject/76377
Search -- https://master.php.net/manage/user-notes.php