dotn30471
2018-04-21 10:42
浏览 116
已采纳

PHP检查文件是否存在于任何目录中

How to check if a file doesn't exist in any directory, add into the $notexists array.

foreach($files as $file){
   foreach ($folders as $this_folder) {
     if (file_exists($this_folder.$file)) {
        $exists[] =$file;
        continue;
     }else{
         // What to do if file is not exist in any directory, add into array.
         $notexists[] = '';
    }
}

图片转代码服务由CSDN问答提供 功能建议

如何检查任何目录中是否存在文件,添加到 $ notexists array。

  foreach($ files as $ file){
 foreach($ folders as this this_folder){
 if(file_exists($ this_folder。$ file)  ){
 $ exists [] = $ file; 
 continue; 
} else {
 //如果文件在任何目录中都不存在该怎么办,添加到数组中。
 $ notexists [] =''  ; 
} 
} 
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • duanqian1888 2018-04-21 10:50
    已采纳

    You need to wait until the end of the loop to know if the file was not found in any of the directories.

    And continue; should be break;. continue restarts starts the next iteration of the current loop, but once you find the file you want to exit the loop completely.

    foreach($files as $file){
        $found = false;
        foreach ($folders as $this_folder) {
            if (file_exists($this_folder.$file)) {
                $exists[] =$file;
                $found = true;
                break;
            }
        }
        if (!$found) {
            // What to do if file is not exist in any directory, add into array.
            $notexists[] = $file;
        }
    }
    
    打赏 评论
  • dongsong4418 2018-04-21 10:52

    You could use array_diff() to check the files that was not found:

    foreach($files as $file){
       foreach ($folders as $this_folder) {
         if (file_exists($this_folder.$file)) {
            $exists[] =$file;
            break;
         }
    }
    $notexists = array_diff($files, $exists);
    
    打赏 评论
  • duanmei4149 2018-04-21 10:54

    you could store them directly after else condition since your using break; you dont have to worry about iteration

    foreach($files as $file){
     foreach ($folders as $folder_path) {
    
       /* NOTE : the '@' here before the file_exists() function 
          is to manage this function errors by yourself after the `else` condition , 
          otherways you can remove it to see the native php errors .
          */
    
       if (@file_exists($folder_path.$file)) {
       $exists[] = $file;
       break;
    
       }else{
       $notexists[] = $file;
       }
     }
    }
    

    RESULT :

    Exist Files      : Array ( [0] => index.php [1] => head.php )
    Non Exist ones : Array ( [0] => random_file.random )

    but be aware that you need to define those arrays before you use them inside the conditions so you dont get undefined errors later if they return an empty array when you loop them as a result .
    so before your first foreach() add those variables definition

    $exists    = array();
    $notexists = array();
    $folders   = array('./'); // your folders list ofc should be here ..
    
    打赏 评论

相关推荐 更多相似问题