note 29932 deleted from function.opendir by sniper
| From: | sniper@php.net | Date: | Sun, 12 Oct 2003 04:24:43 +0000 |
| Subject: | note 29932 deleted from function.opendir by sniper | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-58323@lists.php.net to get a copy of this message | ||
Note Submitter: knuta@partyreg.net
----
<?php
/*
* This little example prints a sorted directory
* listing, with dirs first and files afterwards.
*/
// Open current directory
if($handle = opendir("./")){
// Loop through all files
while(false !== ($file = readdir($handle))){
// Ignore hidden files
if(!preg_match("/^\./", $file)){
// Put dirs in $dirs[] and files in $files[]
if(is_dir($file)){
$dirs[] = $file;
}else{
$files[] = $file;
}
}
}
// Close directory
closedir($handle);
// if $dirs[] exists, sort it and print all elements in it.
if(is_array($dirs)){
sort($dirs);
foreach($dirs as $dir){
echo "<a href=\"$dir/\">$dir/</a><br />\n";
}
}
// if $files[] exists, sort it and print all elements in it.
if(is_array($files)){
sort($files);
foreach($files as $file){
echo "<a href=\"$file\">$file</a><br />\n";
}
}
}
?>