我正在尝试使用RecursiveIteratorIterator
和RecursiveDirectoryIterator
在php中获得文件名,大小和权限。
下面的代码可以正确输出所有文件和目录的名称和大小,但是权限是错误的(使用get $file->getPerms
)。
在这种情况下,输出的所有权限都是相同的,我怀疑0666
只是第一个文件。
还请注意,如果我不使用foreach(new RecursiveIteratorIterator($it) as $file)
,而只是使用foreach($it as $file)
,它可以正常工作,但它不是递归的(也就是它没有显示子目录/文件)。
//remove some file types I don't want showing
$filetypes = array("jpg", "png", "css", "gif");
$it = new RecursiveDirectoryIterator("/root-directory");
foreach(new RecursiveIteratorIterator($it) as $file) {
//foreach($it as $file) {
// ^^This works but it's not recursive ?!
//remove files in $filetypes array
$filetype = pathinfo($file, PATHINFO_EXTENSION);
if (!in_array(strtolower($filetype), $filetypes)) {
//outputs file name correct
echo $file ;
//outputs wrong permissions
echo substr(sprintf('%o', $file->getPerms()), -4);
//outputs file size correct
echo number_format($file->getSize()/1024, 2) . " KB";
}
}
简单地说,这似乎没有在Wamp堆栈上返回准确的结果,可能是一个bug。