我正在尝试将几个php文件合并为一个更大的php文件。
我发送表单数据到一个小文件,并使用包括加载到他们在一个主页。
每次我做一个检查项目的时候,有很多这样的文件,在命令提示符下花费的时间会越来越多。
我能够使用"cat"命令将页面与命令提示符组合起来,并拉出尚未完成的页面。
cat page-1.php page-2.php page-3.php page-4.php page-5.php page-6.php page-7.php page-8.php page-9.php page-10.php page-11.php page-12.php >> newpage.php
我需要php的帮助来执行此操作,而不必从php执行shell命令。
我还需要代码忽略丢失的文件并继续处理其余的文件。
Thanks in advance
foreach (glob("*.php") as $filename) {
$code.=file_get_contents("./$filename");
}
file_put_contents("./combined.php",$code);
在目标目录中运行。它读取目录中所有的php文件,提取它们的代码,然后合并并保存在新文件中。
这将保持它们需要的顺序。另一种方法不能保持它们的顺序,并且会显示错误。
<?php
$txt1 = file_get_contents('page-001.php');
$txt1 .= "'n" . file_get_contents('page-002.php');
$txt1 .= "'n" . file_get_contents('page-003.php');
$txt1 .= "'n" . file_get_contents('page-004.php');
$txt1 .= "'n" . file_get_contents('page-005.php');
$txt1 .= "'n" . file_get_contents('page-006.php');
$txt1 .= "'n" . file_get_contents('page-007.php');
$txt1 .= "'n" . file_get_contents('page-008.php');
$txt1 .= "'n" . file_get_contents('page-009.php');
$txt1 .= "'n" . file_get_contents('page-010.php');
$txt1 .= "'n" . file_get_contents('page-011.php');
$txt1 .= "'n" . file_get_contents('page-012.php');
$fp = fopen('newcombined.php', 'w');
if(!$fp)
die('Could not create / open text file for writing.');
if(fwrite($fp, $txt1) === false)
die('Could not write to text file.');
echo 'Text files have been merged.';
?>