我有这个代码从指定的目录中读取文件,并将内容输出到下拉菜单中。问题是所有文件都没有以字母数字顺序列出。他们都混乱了!无论如何,这是代码:
<form name="index1">
<select name="menu" onchange="jump(this.form)">
<?php
$dir = opendir($dirname);
echo '<option value="">Choose a chapter or volume</option>';
while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != "..") and ($file != "afile.php")){
$name = basename($file,".php");
echo "<option value=".$file.">$name</option>";
}
}
?>
</select>
</form>
我做错了什么?
答案 0 :(得分:1)
这可以解决您的问题:
<form name="index1">
<select name="menu" onchange="jump(this.form)">
<?php
echo '<option value="">Choose a chapter or volume</option>';
// Get an array of files in the "$dirname" path
// The array should be sorted by name ascending
$files = array_filter(glob($dirname. '/*'), 'is_file');
foreach ($files as $file) {
if ($file != "afile.php") {
$name = basename($file,".php");
echo "<option value=".$file.">$name</option>";
}
}
?>
</select>
</form>
您可以查看glob功能,这非常有用!
答案 1 :(得分:-1)
有一个拼写错误:
你写道:foreach(files as $file) {
但它应该是:
foreach($files as $file) {