按字母顺序列出文件(文件和文件夹)
如何使用 PHP 按字母顺序列出文件夹中的所有文件和文件夹?
我对文件 a.txt
、b.txt
、c
和 d.txt
使用了以下内容,其中c
是一个文件夹。问题是 c
显示在最后,而不是在 b.txt
之后,因为它是一个文件夹。
我还希望能够检查每个文件是文件还是文件夹。
<?php
$dir = opendir ("folders");
while (false !== ($file = readdir($dir))) {
echo "$file <br />";
}
?>
How would I list all the files and folders in a folder alphabetically with PHP?
I used the following for the files a.txt
, b.txt
, c
, and d.txt
, Where c
is a folder. The problem is that c
is displayed last instead of after b.txt
because it is a folder.
I'd also like to be able to check if each is either a file or folder.
<?php
$dir = opendir ("folders");
while (false !== ($file = readdir($dir))) {
echo "$file <br />";
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
glob()
的强大功能可以为您提供帮助。只要这样做:The power of
glob()
is here to help you. Just do:只需先将名称读入数组,而不是立即打印。然后对数组进行排序,然后进行输出。
Just read the names into an array first instead of printing immediately. Then sort the array, and then do your output.
我建议使用以下代码(不需要 opendir 等)
对于您问题的第二部分:您使用 php“is_file”和“is_dir”函数
I would suggest the following code (no need for opendir etc.)
For the secound part of your question: You the php "is_file" and "is_dir" functions
只需先将名称读入数组,而不是立即打印。然后对数组进行排序,然后进行输出。
Just read the names into an array first instead of printing immediately. Then sort the array, and then do your output.