简体   繁体   English

为什么scandir()为目录返回false?

[英]Why is scandir() returning false for directories?

I have a directory named client_images that has dir_1 , dir_2 and dir_3 as subdirectories. 我有一个名为client_images的目录,其中dir_1dir_2dir_3作为子目录。 When i run the following: 当我运行以下内容时:

foreach(scandir("client_images") as $key => $value ){
    if(is_dir($value)){
        echo $value." is a dir.<br>";
    }
    else{
        echo $value." is not a dir.<br>";
    }
}

i get : 我得到:

 . is a dir. .. is a dir. dir_1 is not a dir. dir_2 is not a dir. dir_3 is not a dir. 

Why is php returning false for directories? 为什么php为目录返回false?

You are doing scandir("client_images") ; 你正在做scandir("client_images") ; the result is bare filenames without the client_images/ path, so it's looking for them in the current directory. 结果是没有 client_images/ path的裸文件名,所以它在当前目录中查找它们。 Naturally, the only directories likely to be in common between . 当然,唯一的目录可能是共同的. . and client_images/ are . client_images/. and .. , the entries present in all directories. .. ,所有目录中的条目。

You can scan directory using DirectoryIterators . 您可以使用DirectoryIterators扫描目录。 Use the below code to to check if a file is a directory or not 使用以下代码检查文件是否是目录

$iterator = new DirectoryIterator('path');
foreach ($iterator as $fileinfo) {
    if ($fileinfo->isDir()) {
        echo $fileinfo->getFilename() . "\n";
    }
} 
$mydir = "client_images/";

foreach(scandir($mydir) as $key => $value ){
  if(is_dir($mydir . $value)){
    echo $value." is a dir.<br>";
    }
  else{
    echo $value." is not a dir.<br>";
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM