【问题标题】:PHP Help with "if" statement to dynamically include files带有“if”语句的 PHP 帮助以动态包含文件
【发布时间】:2011-02-13 02:26:44
【问题描述】:

我有这些文件:

“id_1_1.php”、“id_1_2.php”、“id_1_3.php”等 “id_2_1.php”、“id_2_2.php”、“id_2_3.php”等

文件数量未知,因为会一直增长..

所有文件都在同一个目录中..

我想做一个if语句:

  1. 仅包含名称以“_1”结尾的文件
  2. 另一个加载所有以“id_1”开头的文件的函数

我该怎么做?谢谢!

edit1:不,数字不会被跳过,一旦我有另一个用于 id_1_ 产品集合的项目,我将添加新项目,如 id_1_1、id_1_2 等。所以不要跳过..

【问题讨论】:

  • 数字可以跳过吗? EG 你可以有 id_1_2.php 和 id_1_4.php 但没有 id_1_3.php 吗?另外,您是否只会寻找“1”?或者您是否想找到所有 id_2_X.php 或 id_X_3.php 文件并包含它们?
  • @aslum,如果可以跳过数字,那么如果不获取目录中所有文件的列表,就没有任何简单的方法可以知道哪些文件确实存在。我认为情况并非如此。
  • 如果文件被自动生成到同一个目录并且会继续增长,我强烈建议您考虑文件系统的目录大小限制。您只能在单个目录中存储最大数量的文件。确保您要在这些限制内进行扩展;)另外,我不知道您的确切情况,但如果这些文件是在某处自动生成的,这与使用 eval() 一样危险(如果不是更多)。
  • edit1:不,数字不会被跳过,一旦我有另一个 id_1_ 产品集合的项目,我将添加新项目,如 id_1_1、id_1_2 等。所以不要跳过.. 的每个新项目集合只是产品颜色版本,因此每个 id_1_ id_2_ 等大约有 4 或 5 个。

标签: php dynamic file include


【解决方案1】:
// Each of these:
//   - scans the directory for all files
//   - checks each file
//   - for each file, does it match the pattern described
//   - if it does, expand the path
//   - include the file once

function includeFilesBeginningWith($dir, $str) {
    $files = scandir($dir);
    foreach ($files as $file) {
        if (strpos($file, $str) === 0) {
            $path = $dir . '/' . $file;
            include_once($path);
        }
    }
}

function includeFilesEndingWith($dir, $str) {
    $files = scandir($dir);
    foreach ($files as $file) {
        if (strpos(strrev($file), strrev($str)) === 0) {
            $path = $dir . '/' . $file;
            include_once($path);
        }
    }
}

/* To use: - the first parameter is ".",
   the current directory, you may want to
   change this */
includeFilesBeginningWith('.', 'id_1');
includeFilesEndingWith('.', '_1.php');

【讨论】:

  • 很好的解释,对于像我这样的新手来说真的很珍贵.. muchas gracias senor :)
  • 德纳达!这对我来说也是一个有用的练习,我以前用exec 做这个,并在里面做一个lsscandir 是一个更好的起点。
【解决方案2】:

大致基于 Svisstack 的原始答案(未经测试):

function doIncludes($pre='',$post=''){
    for ($i=1;1;$i++)
        if (file_exists($str=$pre.$i.$post.'.php'))
            include($str);
        else
            return;
}

function first_function(){
    doIncludes('id_','_1');
}

function second_function(){
    doIncludes('id_1_');
}

【讨论】:

    【解决方案3】:
    function my_include($f, $s)
    {
        @include_once("id_" . $f . "_" . $s . ".php");
    }
    
    
    function first_function($howmany = 100, $whatstart = '1')
    {
        for ($i=1; $i <= $howmany; $i++)
        {
            my_include('1', $i)
        }
    }
    
    function second_function($howmany = 100, $whatend = '1')
    {
        for ($i=1; $i <= $howmany; $i++)
        {
            my_include($i, '1');
        }
    }
    

    【讨论】:

    • 您可能希望这两个函数都从 2 开始,并手动检查 id_1_1.php。否则,如果您同时运行这两个函数,您将包含 1_1 两次。
    • “文件的数量未知,因为会一直增长..” - 你的代码并没有真正做任何事情。
    • @aslum:这个文件只包含一次。
    • @increditman:是的,我没读过,或者这是在编辑中添加的。
    【解决方案4】:

    这将解析每个文件递增一,直到找到一个不存在的文件。假设连续数字它应该捕获每个现有文件。如果要包含名称中数字不是 1 的文件,只需根据需要更改 $lookingfor。

    $lookingfor = 1;
    $firstnum=1;
    while ($firstnum>0) {
    $secondnum=1;
      while ($secondnum>0) {
        $tempfilename = "id_".$firstnum."_".$secondnum.".php";
        if file_exists($tempfilename) {
          if (($firstnum==$lookingfor)||($secondnum==$lookingfor)) {include $tempfilename; }
          $secondnum++;
        } else {
        $secondnum=-1;
        }
      }
    $firstnum++;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      相关资源
      最近更新 更多