php scandir()-为foreach()提供的参数无效[文件]

php scandir()-为foreach()提供的参数无效[文件],php,glob,scandir,Php,Glob,Scandir,我有一个运行PHP 5.4.16版的服务器,我正在尝试使用scandir列出目录中的文件。我很难弄清楚问题是什么。我尝试了../store/dir\u to\u扫描和/root/store/dir\u to\u扫描。我也尝试过同时使用glob和scandir,正如你在下面看到的那样,两者都没有用。如果我删除dir_to_scan目录,它将列出/root/store中的目录,这是我发现最令人费解的。我还将文件夹和文件chmod到777,以确保这不是权限问题。在使用正确的目录设置运行时,我还收到一个

我有一个运行PHP 5.4.16版的服务器,我正在尝试使用scandir列出目录中的文件。我很难弄清楚问题是什么。我尝试了../store/dir\u to\u扫描和/root/store/dir\u to\u扫描。我也尝试过同时使用glob和scandir,正如你在下面看到的那样,两者都没有用。如果我删除dir_to_scan目录,它将列出/root/store中的目录,这是我发现最令人费解的。我还将文件夹和文件chmod到777,以确保这不是权限问题。在使用正确的目录设置运行时,我还收到一个错误“Array([type]=>2[message]=>为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());
?>

这可能很愚蠢,但请尝试在以下内容的末尾提供一个尾随斜杠:

/根/存储/目录到扫描->

$files = scandir("/root/store/dir_to_scan/");

这应该能解决你的问题

$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);
        }
    }

我感谢您的回复,不幸的是,无论是否带有尾随斜杠,都存在相同的问题/是我用来解决这个问题的代码,我住在西班牙。。而$地毯是唯一的西班牙语wordWell,这让我离成功更近了一步。它可以像其他脚本一样读取/root/store,但不会在/root/store/dir\u to\u扫描中列出文件。也就是说,它不再抛出“数组([type]=>2[message]=>为foreach()[file]=>/root/html/test.php[line]=>5]提供的参数无效”错误。可能是权限问题,请检查它。。也可以尝试这样做:echo exec(“ls/root/store/dir_to_scan”);drwxr-xr-x 2 root root 4096 Jun 8 09:27 dir_to_scan drwxr-xr-x 2 root 4096 Jun 8 07:35 dir_to_scann2 drwxr-xr-x 2 root 4096 Jun 8 07:18 dir_to_scann 3 drwxr-xr-x 2 root 4096 Jun 7 22:18 dir_to_to_scan4如果使用dir_to_to_scann2?函数,还可以将用户/组dir和文件更改为apache。