我有一台运行 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());
?>