2010-04-27 43 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++; 
}