列出一个目录中的所有文件 PHP

问题描述:

使用 PHP 列出一个目录中的所有文件的最佳方法是什么?是否有 $_SERVER 函数来执行此操作?我想列出 usernames/目录中的所有文件,并使用链接循环遍历该结果,以便我只需单击文件名的超链接即可到达那里.谢谢!

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!

看看这个:readdir()一>

这段代码应该列出某个目录中的所有条目:

Check this out : readdir()

This bit of code should list all entries in a certain directory:

if ($handle = opendir('.')) {

    while (false !== ($entry = readdir($handle))) {

        if ($entry != "." && $entry != "..") {

            echo "$entry
";
        }
    }

    closedir($handle);
}

编辑:miah 的解决方案比我的要优雅得多,你应该改用他的解决方案.


Edit: miah's solution is much more elegant than mine, you should use his solution instead.