note 49825 deleted from function.scandir by danbrown
| From: | danbrown@php.net | Date: | Thu, 28 Jan 2010 14:17:49 +0000 |
| Subject: | note 49825 deleted from function.scandir by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-165451@lists.php.net to get a copy of this message | ||
Note Submitter: rich at jamsoft dot biz
----
Ok, in my previous example, you need to do the SORT after you remove the "." and
".." entries from the array. Otherwise, you cannot start using the array at 0.
Here is corrected code:
<?php
function scandir($dir, $no_dots=FALSE) {
$files = array();
$dh = @opendir($dir);
if ($dh!=FALSE) {
while (false !== ($filename = readdir($dh))) {
$files[] = $filename;
}
if ($no_dots) {
while(($ix = array_search('.',$files)) > -1)
unset($files[$ix]);
while(($ix = array_search('..',$files)) > -1)
unset($files[$ix]);
}
sort($files);
}
return $files;
}
?>