2010-04-27 55 views
1

我有这些文件:PHP帮,带 “if” 语句来动态包含文件

“id_1_1.php”, “id_1_2.php”, “id_1_3.php” 等 “id_2_1.php”,“id_2_2 .PHP”,‘id_2_3.php’等

文件是不知道,因为总会长的数量..

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

我想如果做一个声明:

  1. 包括仅当它们的名称以“_1”结尾的文件
  2. 另一个函数加载所有以“ID_1”启动文件

我怎样才能做到这一点?谢谢!

EDIT1:没有数字将不会被跳过,一旦我有另一个项目为id_1_收集的产品,我会添加新的作为id_1_1,id_1_2等..所以没有跳跃..

+1

能号码被跳过? EG你可以有id_1_2.php和id_1_4.php,但没有id_1_3.php? 另外,你只会一直在寻找“1”?或者你可能想要找到所有的id_2_X.php或id_X_3.php文件并包含它们? – aslum 2010-04-27 22:04:46

+0

@aslum,如果数字可以跳过,那么在没有获得目录中所有文件的列表的情况下,确实无法知道哪些文件存在。我不认为是这样。 – Cam 2010-04-27 22:07:07

+0

如果这些文件正在自动生成到同一个目录并且将不断增长,我强烈建议您考虑文件系统的目录大小限制。您只能将最大数量的文件存储在单个目录中。请确保你要在这些限制内进行缩放;) 另外,我无法确切知道你的确切情况,但是如果这些文件是在某处自动生成的,这与使用'eval(如果不是更多) )'。 – d11wtq 2010-04-28 00:12:56

回答

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'); 
+0

很好的解释,对于像我这样的新手来说真的很珍贵。 muchas gracias senor :) – 2010-04-28 13:36:12

+0

De nada!对我来说这也是一个有益的练习,我曾经用'exec'和''ls'做了这个。“scandir”是一个更好的起点。 – artlung 2010-04-28 15:35:22

1
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'); 
    } 
} 
+0

您可能希望将这两个函数的开头设置为2,并手动检查id_1_1.php。否则,如果你运行这两个函数,你将包含1_1两次。 – aslum 2010-04-27 22:03:02

+1

“文件数量不知道,因为总是会增长..” - 你的代码并没有真正做到这一点。 – Cam 2010-04-27 22:05:37

+0

@aslum:这个文件只包含一次。 – Svisstack 2010-04-27 22:39:40

1

基于松散在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_'); 
} 
1

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

$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++; 
}