我们有一些html代码,比如:
<body>Some text</body>
以及一个变量CCD_ 1。
这是我第一次在php中使用zip,有几个问题。
如何:
创建一个名为
HTML
的文件夹,并将其放置在$contents
中(在ftp上没有实际创建,只是在变量中)创建一个
index.html
并将其放置在HTML
文件夹中,该文件夹位于$contents
中因此zip之前的
$contents
应该包含:/HTML/index.html (with <body>Some text</body> code inside)
创建一个zip存档,其中包含
$contents
变量中的所有内容。
如果我理解正确:
$contents = '/tmp/HTML';
// Make the directory
mkdir($contents);
// Write the html
file_put_contents("$contents/index.html", $html);
// Zip it up
$return_value = -1;
$output = array();
exec("zip -r contents.zip $contents 2>&1", $output, $return_value);
if ($return_value === 0){
// No errors
// You now have contents.zip to play with
} else {
echo "Errors!";
print_r($output);
}
我没有使用库来压缩它,只是使用命令行,但如果您愿意,可以使用库(但我正在检查zip
是否正确执行)。
如果你真的想在记忆中做每件事,你可以这样做:
$zip = new ZipArchive;
if ($zip->open('contents.zip') === TRUE) {
$zip->addFromString('contents/index.html', $html);
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
http://www.php.net/manual/en/ziparchive.addfromstring.php
我建议使用$contents
0类。所以你可以有这样的
$html = '<body>some HTML</body>';
$contents = new ZipArchive();
if($contents->open('html.zip', ZipArchive::CREATE)){
$contents->addEmptyDir('HTML');
$contents->addFromString('index.html', $html);
$contents->close()
}