如何使用PHP列出目录中的文件

时间:2018-09-20 22:27:45

标签: php list file directory

我搜索了所有可以在Internet上找到并删除的示例PHP代码。如果有人可以帮忙,那就太好了。
目录路径是我根目录下的子目录,  ( root / images)或( root / pdffiles)。我想给出一个文件列表,可以使用循环功能下载这些文件。 scandir()看起来可能是个窍门,但是不知何故,语法一定在帮我忙。无论我如何构造代码,我一无所获。

1 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:

<?php
$dir = "test/filedemo";
if (is_dir($dir)){
  if ($dh = opendir($dir)){
    while (($file = readdir($dh)) !== false){
      echo "filename:" . $file . "<br>";
    }
    closedir($dh);
  }
}
?>