如何使用 PHP 按字母顺序列出目录中的所有文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3977500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I list all files in a directory sorted alphabetically using PHP?
提问by David B
I'm using the following PHP code to list all files and folders under the current directory:
我正在使用以下 PHP 代码列出当前目录下的所有文件和文件夹:
<?php
$dirname = ".";
$dir = opendir($dirname);
while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != "..") and ($file != "index.php"))
{
echo("<a href='$file'>$file</a> <br />");
}
}
?>
The problem is list is not ordered alphabetically (perhaps it's sorted by creation date? I'm not sure).
问题是列表不是按字母顺序排列的(也许它是按创建日期排序的?我不确定)。
How can I make sure it's sorted alphabetically?
我怎样才能确保它按字母顺序排序?
回答by codaddict
The manualclearly says that:
该手册明确说:
readdir
Returns the filename of the next file from the directory. The filenames are returned in the order in which they are stored by the filesystem.
readdir
返回目录中下一个文件的文件名。文件名按文件系统存储它们的顺序返回。
What you can do is store the files in an array, sort it and then print it's contents as:
您可以做的是将文件存储在一个数组中,对其进行排序,然后将其内容打印为:
$files = array();
$dir = opendir('.'); // open the cwd..also do an err check.
while(false != ($file = readdir($dir))) {
if(($file != ".") and ($file != "..") and ($file != "index.php")) {
$files[] = $file; // put in array.
}
}
natsort($files); // sort.
// print.
foreach($files as $file) {
echo("<a href='$file'>$file</a> <br />\n");
}
回答by Vitim.us
<?php
function getFiles(){
$files=array();
if($dir=opendir('.')){
while($file=readdir($dir)){
if($file!='.' && $file!='..' && $file!=basename(__FILE__)){
$files[]=$file;
}
}
closedir($dir);
}
natsort($files); //sort
return $files;
}
?>
<html>
<head>
</head>
<body>
<h1> List of files </h1>
<ul class="dir">
<? foreach(getFiles() as $file)
echo "<li name='$file'><a href='$file'>$file</a></li>";
?>
</ul>
</body>
</html>
回答by cored
You could put all the directory names inside an array like:
您可以将所有目录名称放在一个数组中,例如:
$array[] = $file;
After that you can sort the array with:
之后,您可以使用以下命令对数组进行排序:
sort($array);
And then print the links with that content.
然后打印包含该内容的链接。
I hope this help.
我希望这会有所帮助。
回答by Andrew Sledge
<?php
$dirname = ".";
$dir = opendir($dirname);
while(false != ($file = readdir($dir)))
{
if(($file != ".") and ($file != "..") and ($file != "index.php"))
{
$list[] = $file;
}
}
sort($list);
foreach($list as $item) {
echo("<a href='$item'>$item</a> <br />");
}
?>
回答by Jan.
I'd recommend moving away from the old opendir()/readdir(). Either use glob() or if you encounter a lot of files in a directory then use the DirectoryIterator Class(es):
我建议远离旧的 opendir()/readdir()。使用 glob() 或者如果您在目录中遇到很多文件,请使用 DirectoryIterator Class(es):
http://www.php.net/manual/en/class.directoryiterator.phphttp://www.php.net/manual/en/function.glob.php
http://www.php.net/manual/en/class.directoryiterator.phphttp://www.php.net/manual/en/function.glob.php
Regards
问候
回答by Armesh Singh
You can use this beautiful script:
你可以使用这个漂亮的脚本:
http://halgatewood.com/free-php-list-files-in-a-directory-script/
http://halgatewood.com/free-php-list-files-in-a-directory-script/