将HTML文件中的内容包含到一个主HTML文件中,然后下载它


include content from html files into one main html file and then download it

我想创建一个页面,我有一些复选框,如:添加标题:

"Add header-1", "Add header-2", "Add header-3"

添加内容:

"Add content-1", "Add content-2", "Add content-3"

对于这些复选框中的每一个对应一个html文件,例如"header-1"对应服务器上名为"header-1.html"的文件,其他文件也是如此。

我想做的是添加一个名为"下载"的按钮,在用户检查之后,让我们说"add header-1"answers"add-content-1",然后点击"下载",他们将收到一个存档,其中选定的文件合并到一个名为"index.html"的主文件中。

这是我想要达到的目标的总体视图。

这是可能使用PHP吗?如果是这样,你能给我一些建议,从哪里开始呢?

您可以做的是对每个已选择的文件使用file_get_contents(),然后将这些字符串连接在一起。您必须想出一种方法来确定哪个文件已被选中,但随后您只需将文件名替换为变量。

$fileOne = file_get_contents(file1.html);
$fileTwo = file_get_contents(file2.html);
$fileThree = file_get_contents(file3.html);
$fileToDownload = $fileOne.$fileTwo.$fileThree; 

然后写入文件:

$rHandler = fopen('my-new-file.html', r+);
fwrite($rHandler, $fileToDownlod);
fclose($rHandler);

然后提供一个链接到你写HTML文件的地方。

<a href='my-new-file.html'>Download here</a>

更新

您想提供一个zip文件。摘自http://php.net/manual/en/zip.examples.php

<?php
$zip = new ZipArchive();
$filename = "./my-html-files.zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
    exit("cannot open <$filename>'n");
}
$zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as      testfilephp.txt.'n");
$zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.'n");
$zip->addFile($thisdir . "/too.php","/testfromfile.php");
echo "numfiles: " . $zip->numFiles . "'n";
echo "status:" . $zip->status . "'n";
$zip->close();
?>

然后提供一个新的下载链接

<a href='my-html-file.zip'>Download here</a>