note 54230 added to function.readdir

From: Date: Tue, 28 Jun 2005 03:37:39 +0000
Subject: note 54230 added to function.readdir
Groups: php.notes 
Request: Send a blank email to php-notes+get-91048@lists.php.net to get a copy of this message
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"; ?> ---- Manual Page -- http://www.php.net/manual/en/function.readdir.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+54230 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+54230&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+54230&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+54230&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+54230&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+54230&report=yes&reason=non-english Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+54230&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+54230&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#91048) next »