【问题标题】:PHP - include files from array only if all exist in directoryPHP - 仅当所有文件都存在于目录中时才包含数组中的文件
【发布时间】:2018-01-02 12:32:58
【问题描述】:

我正在尝试创建一个包含(通过 require_once)多个文件的脚本,但我期望它具有以下行为:

  • 所需文件的所有文件名都定义为数组中的值
  • 脚本检查数组中的所有文件是否存在于给定目录中
  • 如果是,要求它们并继续(仅当它们都存在时)
  • 如果否,终止脚本并显示错误消息(如果缺少任何文件)

更新

仔细查看我的原始脚本后,我发现它为什么不起作用。第二个 IF 语句 ($countMissing == 0) 在 FOR 循环内,它为找到的文件生成空数组。将 IF 语句从循环中取出来解决问题。

工作版本(稍作修改):

// Array with required file names
$files = array('some_file', 'other_file', 'another_file');
// Count how many files is in the array
$count = count($files);
// Eampty array for catching missing files
$missingFiles = array();

for ($i=0; $i < $count; $i++) {
    // If filename is in the array and file exist in directory...
    if (in_array($files[$i], $files) && file_exists(LIBRARIES . $files[$i] . '.php')) {
        // ...update array value with full path to file
        $files[$i] = LIBRARIES . $files[$i] . '.php';
    } else {
        // Add missing file(s) to array
        $missingFiles[] = LIBRARIES . $files[$i] . '.php';      
    }
}

// Count errors
$countMissing = count($missingFiles);
// If there was no missing files...
if ($countMissing == 0) {
    foreach ($files as $file) {
        // ...include all files
        require_once ($file);
    }
} else {
    // ...otherwise show error message with names of missing files
    echo "File(s): " . implode(", ", $missingFiles) . " wasn't found.";
}

如果这个帖子不会被删除,我希望它对某人有所帮助。

【问题讨论】:

    标签: php arrays if-statement include


    【解决方案1】:

    试试这个:

    $files = array(
        'some_file', 
        'other_file', 
        'another_file',
    );
    
    // create full paths
    $files = array_map(function ($file) {
        return ROOT_DIR . $file . '.php')
    }, $files);
    
    // find missing files
    $missing = array_filter($files, function ($file) {
        return !file_exists($file);
    });
    
    if (0 === count($missing)) {
        array_walk($files, function ($file) {
            require_once $file;
       });
    } else {
        array_walk($missing, function ($file) {
            echo "File: " . $file " wasn't found.";
        });
    }
    

    参考见:

    【讨论】:

      【解决方案2】:

      试试这个,防止循环内循环。

      for ($i=0; $i < $count; $i++) {
          // If filename is in the array but file not exist in directory...
          if (in_array($files[$i], $files) && !file_exists(ROOT_DIR . $files[$i] . '.php')) {
              // ...add name of missing file to error array
              $errors[] = $files[$i];
          }
          else{
      
              require_once (ROOT_DIR . $file[$i] . '.php');
      
          }
        }
      

      【讨论】:

      • 我从这样的脚本开始,但它不起作用,这就是为什么我尝试添加另一个循环作为错误检查。它的行为就像它忽略 IF 并且只执行 ELSE 而不是破坏页面上的其余脚本,因为缺少一些必需的文件。
      【解决方案3】:

      localheinz 和 jp 的代码可能没问题,但我不会编写这样的代码,因为它会使事情变得复杂。假设您不想要丢失文件的列表(这会略有不同),我会这样做:

      $filesOK=true;
      foreach($files as $file)
      {
         $path = ROOT_DIR . $file . ".php";
      
         if(!file_exists($path )) 
         {
             $filesOK=false; // we have a MIA
             break;          // quit the loop, one failure is enough 
         }
      }
      
      if($filesOK)
         foreach($files as $file)
            require_once($file);
      else
         echo "We have a problem";
      

      对我来说,这更容易一目了然。更容易调试,CPU 将以一种或另一种方式完成相同的工作。执行速度可能没有太大差异 - 如果这很重要的话。

      如果你需要丢失文件的列表,那么:

      $filesOK=true;
      foreach($files as $file)
      {
         $path = ROOT_DIR . $file . ".php";
      
         if(!file_exists($path)) // assume each file is given with proper path
         {
             $filesOK=false; // we have a MIA
             $mia[]=$path;   // or $file if you just want the name
         }
      }
      
      if($filesOK)
         foreach($files as $file)
            require_once($file);
      else
      {
         if(is_array(@$mia))         // I always make sure foreach is protected
           foreach($mia as $badfile) // even if it seems obvious that its ok 
              echo "Missing in action: $badfile<br>";
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-23
        • 1970-01-01
        • 2014-09-05
        • 1970-01-01
        • 1970-01-01
        • 2021-01-05
        • 2021-04-18
        相关资源
        最近更新 更多