// 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');
能号码被跳过? 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
@aslum,如果数字可以跳过,那么在没有获得目录中所有文件的列表的情况下,确实无法知道哪些文件存在。我不认为是这样。 – Cam 2010-04-27 22:07:07
如果这些文件正在自动生成到同一个目录并且将不断增长,我强烈建议您考虑文件系统的目录大小限制。您只能将最大数量的文件存储在单个目录中。请确保你要在这些限制内进行缩放;) 另外,我无法确切知道你的确切情况,但是如果这些文件是在某处自动生成的,这与使用'eval(如果不是更多) )'。 – d11wtq 2010-04-28 00:12:56