note 29932 added to function.opendir
| From: | knuta at partyreg dot net | Date: | Sat, 01 Mar 2003 03:55:29 +0000 |
| Subject: | note 29932 added to function.opendir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-44686@lists.php.net to get a copy of this message | ||
<?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";
}
}
}
?>
--
http://www.php.net/manual/en/function.opendir.php
http://master.php.net/manage/user-notes.php?action=edit+29932
http://master.php.net/manage/user-notes.php?action=delete+29932
http://master.php.net/manage/user-notes.php?action=reject+29932