PHP通过HTML字符串创建ZIP存档 [英] PHP pass HTML string to create ZIP archive
本文介绍了PHP通过HTML字符串创建ZIP存档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我试图从2个字符串创建一个Zip文件。 1是一个html字符串,另一个是纯文本。到目前为止,我似乎只是使文本字符串正常工作,但应该是HTML的文件只是一个空白文件。任何想法为什么?
So I am trying to create a Zip file from 2 strings. 1 is a html string, the other is plain text. So far I only seem to get the text string to work correctly but the file thats supposed to be HTML is just a blank file. Any idea why?
$string1 = $_POST["html_string"];
$string2 = "Some data Some data Some data Some data Some data Some data";
$filename = "test.zip";
$zip = new ZipArchive();
if ($zip->open($filename, ZIPARCHIVE::CREATE)==TRUE) {
$zip->addFromString("string1.html", $string1);
$zip->addFromString("string2.txt", $string2);
$zip->close();
}
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
//clearstatcache();
header("Content-Length: ".filesize($filename));
readfile($filename);
unlink($filename);
感谢任何帮助。
推荐答案
尝试代码替换
$string1 = $_POST["html_string"];
与
$string1 = '<html>
<title></title>
</html>';
并工作。
你确定$ _POST [html_string]不是空的?添加var_dump($ _ POST);查看是否有一些数据或更新您的代码在开始时添加支票。
Are you sure $_POST["html_string"] is not empty? Add var_dump($_POST); to see if there is some data or update your code adding a check at the begining.
if (empty($_POST["html_string"]))
{
echo 'html_string is empty';
exit;
}
这篇关于PHP通过HTML字符串创建ZIP存档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文