如果我理解正确:
$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
来源
2012-07-08 10:06:26
Jay
mkdir创建文件夹和file_put_contents在ftp上创建文件,我不想做的事情。这个想法是在变量内创建文件夹和文件,并将结果压缩。没有真正的创造,所有的行为都应该记忆。不管怎样,谢谢。 – James 2012-07-08 10:16:28
如果你在'/ tmp'中做了所有的事情,那么技术上可以满足你的要求,因为'/ tmp'是一个安装在RAM(即内存)中的文件系统。不过,请参阅我的编辑,以便在PHP内存中完成整个操作。 – Jay 2012-07-08 10:24:20