我正在尝试创建一个脚本,该脚本将获取多个PHP文件,并使用该脚本创建一个PHP文件。例如home.php,about.php,new.php将合并为Index.php。
我尝试了以下代码:
$code = '';
foreach (glob("*.php") as $filename) {
$code.=file_get_contents("./$filename");
}
file_put_contents("./combined.php",$code);
但不会输出。
如何合并多个PHP文件?
答案 0 :(得分:0)
我已经在计算机上检查了您的代码,看来它运行良好。但是不明白为什么它对您不起作用。
顺便说一句,如果您需要更多解决方案,则还可以尝试以下方式。
如果您的文件来自表单,则可以按以下步骤操作:
$files = [];
$combine_files = $_POST['combine_files'];
foreach ($combine_files as $single_file)
{
$files .= $single_file;
}
$combined_contents;
foreach ($files as $single_file)
{
$combined_contents .= file_get_contents("./".$single_file);
}
file_put_contents("./combined.php",$combined_contents);
如果不是来自表单,那么也可以通过以下方式简单地完成此操作:
$files = ["home.php", "about.php", "new.php"]; // create an array of files
$combined_contents;
foreach ($files as $single_file)
{
$combined_contents .= file_get_contents("./".$single_file);
}
file_put_contents("./combined.php",$combined_contents); // puts all contents into one file