开发者

How to merge HTML files into one file with PHP?

I was wondering if it's possible to merge a bunch of HTML files into one file, dynamically, when a user clicks a link? Let's say we have the following files开发者_如何转开发 in /files

1.html
2.html
3.html
4.html

When a user clicks a link, it takes all of those files and creates a new file that has all of their HTML content merged (but it doesn't delete the originals) using PHP.

Thanks in advance for any suggestions.


<?php

readfile('1.html');
readfile('2.html');
readfile('3.html');
readfile('4.html');


To simply output the files after they have been merged you can use:

chdir("./files/");
print join(array_map("file_get_contents",
                     array("1.html", "2.html", "3.html", "4.html")));

I guess you didn't mean a static list though. To have a complete directory /files read you can adapt this to:

print join(array_map("file_get_contents", glob("files/*.html")));

If your question was really about creating a new file from the combined contents, then replace the print with file_put_contents(..., "5.html") for example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜