列出一个目录中的所有文件 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15774669/
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
List all files in one directory PHP
提问by Shadowpat
What would be the best way to list all the files in one directory with PHP? Is there a $_SERVER function to do this? I would like to list all the files in the usernames/ directory and loop over that result with a link, so that I can just click the hyperlink of the filename to get there. Thanks!
使用 PHP 列出一个目录中的所有文件的最佳方法是什么?是否有 $_SERVER 函数来执行此操作?我想列出 usernames/ 目录中的所有文件,并使用链接遍历该结果,以便我只需单击文件名的超链接即可到达那里。谢谢!
采纳答案by Orel Biton
Check this out : readdir()
This bit of code should list all entries in a certain directory:
看看这个:readdir()
这段代码应该列出某个目录中的所有条目:
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "$entry\n";
}
}
closedir($handle);
}
Edit: miah's solution is much more elegant than mine, you should use his solution instead.
编辑:miah 的解决方案比我的要优雅得多,您应该改用他的解决方案。