note 54230 deleted from function.readdir by tularis
| From: | tularis@php.net | Date: | Sun, 13 May 2007 19:03:16 +0000 |
| Subject: | note 54230 deleted from function.readdir by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126528@lists.php.net to get a copy of this message | ||
Note Submitter: Alex M
----
A shorter and simpler version of twista no[at]$pam rocards $pam[dot]no de
code to first display all folders sorted, and THEN all files sorted. Just like windows.
<?php
$path = $_SERVER[DOCUMENT_ROOT]."/old/";
$dh = @opendir($path);
while (false !== ($file=@readdir($dh)))
{
if (substr($file,0,1)!=".") #skip anything that starts with a '.'
{ #i.e.:('.', '..', or any hidden file)
if (is_dir($path.$file))
$dirs[]=$file.'/'; #put directories into dirs[] and append a '/'
to differentiate
else
$files[]=$file; #everything else goes into files[]
}
}
@closedir($dh);
if ($files)
natcasesort($files); #natural case insensitive sort
if ($dirs)
natcasesort($dirs);
$files=array_merge($dirs,$files); #merge dirs[] and files[] into files with dirs first
foreach ($files as $file) #that's all folks, display sorted all folders and files
echo "$file<br>\n";
?>