我有一个运行PHP版本5.4.16的服务器,我试图使用scandir来列出目录内的文件。我很难弄清楚问题是什么。我都试过了……/store/dir_to_scan和/root/store/dir_to_scan。我也试过同时使用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());
?>
这可能有点傻,但是尝试在末尾提供一个斜杠:
/根/商店/dir_to_scan ->
$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);
}
}