0

我有一台运行 PHP 版本 5.4.16 的服务器,并且正在尝试使用 scandir 列出目录中的文件。我很难弄清楚问题是什么。我已经尝试过 ../store/dir_to_scan 和 /root/store/dir_to_scan。我也尝试过同时使用 glob 和 scandir,正如您在下面看到的那样,都无济于事。如果我删除 dir_to_scan 目录,它将列出 /root/store 中的目录,这是我发现最令人费解的。我还将文件夹和文件更改为 777,以确保这不是权限问题。我在运行时也收到“Array ([type] => 2 [message] => Invalid argument provided for foreach() [file] => /root/html/test.php [line] => 5)”的错误使用正确的目录设置。

谢谢你的帮助。

目录设置

/root/html //where php script is run
/root/store/dir_to_scan //files I need to list

PHP 脚本

<?
 #$files = glob('../store/dir_to_scan/*', GLOB_BRACE);
 $files = scandir("/root/store/dir_to_scan/");
   foreach($files as $file) {

      //do work here
      if ($file === '.' || $file === '..') {
                continue;
       }
    echo $file . "<br>";
}
print_r(error_get_last());
?>
4

2 回答 2

0

this should to solve your problem

$carpeta= "/root/store/dir_to_scan";
    if(is_dir($carpeta)){
        if($dir = opendir($carpeta)){
            while(($file = readdir($dir)) !== false){
                if($file != '.' && $file != '..' && $file != '.htaccess'){
                  echo $file;  //or save file name in an array..
                 //  $array_file[] = $file;

                }
            }
            closedir($dir);
        }
    }
于 2013-06-09T04:47:11.377 回答
0

这可能很愚蠢,但尝试在末尾提供一个斜杠:

/root/store/dir_to_scan ->

$files = scandir("/root/store/dir_to_scan/");
于 2013-06-08T23:51:40.337 回答