列出一个目录中的所有文件 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:56:47  来源:igfitidea点击:

List all files in one directory PHP

phplistdirectory

提问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 的解决方案比我的要优雅得多,您应该改用他的解决方案。

回答by miah

You are looking for the command scandir.

您正在寻找命令scandir

$path    = '/tmp';
$files = scandir($path);

Following code will remove .and ..from the returned array from scandir:

以下代码将从返回的数组中删除.和:..scandir

$files = array_diff(scandir($path), array('.', '..'));