包含文件夹中的所有文件-PHP
我试图在php中包含来自另一个文件夹的所有文件。以下是我当前的目录结构:包含文件夹中的所有文件-PHP,php,include,Php,Include,我试图在php中包含来自另一个文件夹的所有文件。以下是我当前的目录结构: > folder1 main.php > folder2 > folder3 someFile.php someFile2.php someFile3.json 我试着做: include "../folder2/"; 这(仅包括php文件): 从main.php,我想包括所有folder2/,包括子文件夹:folder3,以及folder2/中的php和jso
> folder1
main.php
> folder2
> folder3
someFile.php
someFile2.php
someFile3.json
我试着做:
include "../folder2/";
这(仅包括php文件):
从
main.php
,我想包括所有folder2/
,包括子文件夹:folder3
,以及folder2/
中的php和json文件。我已经研究了其他堆栈溢出问题,了解了for循环方法,但还没有找到一种包含不同文件类型(.php、.json等)和子目录的方法。感谢您的帮助。谢谢 PHP的include
不应用于其他文件类型,如.json
。要从这些文件中提取数据,您需要使用类似于file\u get\u contents
的方法读取它们。例如:
$data=json\u decode(文件获取内容('someFile3.json');
要递归地将PHP文件包含在其他目录中,您可以尝试递归地搜索所有目录:
function require_all($dir, $max_scan_depth, $depth=0) {
if ($depth > $max_scan_depth) {
return;
}
// require all php files
$scan = glob("$dir/*");
foreach ($scan as $path) {
if (preg_match('/\.php$/', $path)) {
require_once $path;
}
elseif (is_dir($path)) {
require_all($path, $max_scan_depth, $depth+1);
}
}
}
$max_depth = 255;
require_all('folder3', $max_depth);
此代码是此处代码的修改版本:PHP的
include
不应用于其他文件类型,如.json
。要从这些文件中提取数据,您需要使用类似于file\u get\u contents
的方法读取它们。例如:
$data=json\u decode(文件获取内容('someFile3.json');
要递归地将PHP文件包含在其他目录中,您可以尝试递归地搜索所有目录:
function require_all($dir, $max_scan_depth, $depth=0) {
if ($depth > $max_scan_depth) {
return;
}
// require all php files
$scan = glob("$dir/*");
foreach ($scan as $path) {
if (preg_match('/\.php$/', $path)) {
require_once $path;
}
elseif (is_dir($path)) {
require_all($path, $max_scan_depth, $depth+1);
}
}
}
$max_depth = 255;
require_all('folder3', $max_depth);
此代码是此处代码的修改版本: