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

时间:2014-08-03 17:10:45

标签: javascript php jquery html

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

"添加标题-1","添加标题-2" "添加标题-3"

添加内容:

"添加内容-1","添加内容-2" "添加内容-3"

和#34;页脚"

相同

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

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

这是我想要实现的目标的整体观点。

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

1 个答案:

答案 0 :(得分:0)

您可以做的是,在已选择的每个文件上使用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>