开发者

Got error when putting DirectoryIterator() inside a for loop in PHP!

This is my code to go through all special folder "folderContainer" which inside other drives:

$folderNames = '';
// go through all drives.Just want to find out which has folderContainer
for($i='D';$i<='Z';$i++){ 
     // get the path    
     $path = realpath($i.':\\folderContainer\\');

     $folders = new DirectoryIterato开发者_运维百科r($path);

     foreach($folders as $folder){
         if($folder->isDot()) continue;
         // get folder's name which match my pattern
         if ($folder->isDir() && preg_match('/^[A-Z]{1}[0-9]{1,9}$/i', $folders)){
              // store the folder name
              $folderNames .= ' '.$folder.' ';
          }
     } 
} 

And got these error messages:

Fatal error: Uncaught exception 'RuntimeException' with message 'Directory name must not be empty.' 

But my code work correct when I set the $path to "D:\\folderContainer\\" without a loop. So, how can I to make the code to go through all folderContainer which inside other drives .

Thank you very much!!


realpath returns null if the path doesn't exist. And probably you want to skip not existing folders

//...
$path = realpath($i.':\\folderContainer\\');
if (empty($path))
    continue;
//...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜