Sorting files in a directory
| From: | Steve Marquez | Date: | Wed, 08 Aug 2007 19:43:50 +0000 |
| Subject: | Sorting files in a directory | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-260454@lists.php.net to get a copy of this message | ||
Greetings,
I have some code that opens a directory and displays the files. I want to
display the files in Alphabetical order.
I know this code does not work, but I was curious if someone can take a look
and tell me what is wrong? Thank you so much.
<form name="select_page" method="post" action="">
<?php
// set pattern
$pattern = ".html*|.php*";
// open directory and parse file list
if (is_dir("files/"))
{
if ($dh = opendir("files/"))
{
echo "<select name=\"file\" size=\"8\">";
//echo "<option value=\"$file\" selected>$file</option>";
// iterate over file list
while (($file = readdir($dh)) !== false)
{
// if filename matches search pattern, print it
if (ereg($pattern, $file))
if(strpos($file,'.')>0)
// if ($filename = str_replace('.html','',$filename))
{
$file_array = array($file);
sort ($file_array);
foreach($file_array as $key => $value) {
echo "<option value=\"$value\">".$value."</option>";
}
}
}
echo "</select>";
// close directory
closedir($dh);
}
}
?>
<p>
<input type="submit" name="Submit" value="Submit">
</p>
</form>
--
Steve Marquez