Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何按名称对php目录列表进行排序?_Php_Sorting_Directory - Fatal编程技术网

如何按名称对php目录列表进行排序?

如何按名称对php目录列表进行排序?,php,sorting,directory,Php,Sorting,Directory,所以,我只是希望我的webdrive文件夹是“可浏览的”,所以我找到了这个脚本为我做这件事。 问题是,列表没有按名称排序 如何修改代码以便按文件名排序 <?php $dir_open = opendir('.'); while(false !== ($filename = readdir($dir_open))){ if($filename != "." && $filename != ".."){ $lin

所以,我只是希望我的webdrive文件夹是“可浏览的”,所以我找到了这个脚本为我做这件事。 问题是,列表没有按名称排序 如何修改代码以便按文件名排序

<?php
$dir_open = opendir('.');

while(false !== ($filename = readdir($dir_open))){
    if($filename != "." && $filename != ".."){
        $link = "<a href='./$filename'> $filename </a><br />";
        echo $link;
    }
}

closedir($dir_open);
?>

您可以将文件名存储在数组中,然后使用

$files=[];
$dir_open=opendir('.');
while(false!==($filename=readdir($dir\u open))){
如果($filename!=“&&&$filename!=”){
$files[]=$filename;//存储文件名
}
}
closedir($dir_open);
//然后,进行排序和显示
排序($文件);
foreach($files作为$filename){
$link=“
”; echo$link; }
非常感谢您!如果我想进行不同的排序(例如,按日期),我将如何进行排序?您还可以在数组中存储
filemtime()
信息。并使用
usort()
$files[$filename]=filemtime($filename)进行排序然后
asort($files)
。有很多方法,但不容易在评论中描述;)
$files = [];
$dir_open = opendir('.');
while(false !== ($filename = readdir($dir_open))){
    if($filename != "." && $filename != ".."){
        $files[] = $filename; // store filename
    }
}
closedir($dir_open);

// Then, sort and display
sort($files);

foreach ($files as $filename) {
    $link = "<a href='./$filename'> $filename </a><br />";
    echo $link;
}