我有这个代码
<?PHP $files = array('includes/s.xlsx','includes/test.html'); $zipname = 'file.zip'; $zip = new ZipArchive; $zip->open($zipname,ZipArchive::CREATE); foreach ($files as $file) { $zip->addFile($file); } $zip->close(); header('Content-Type: application/zip'); header('Content-disposition: attachment; filename=filename.zip'); header('Content-Length: ' . filesize($zipname)); readfile($zipname);@H_301_3@但我不想将zip文件保存到服务器,我只是想将它提供给用户.
ZIP不是可流式格式(目标需要支持搜索,即ZIP文件不能简单地写入无法重新读取和修改以前写入的数据的目标),您尝试做的事情无法正常工作.所以最好的解决方案(如果你的zip文件不会很大)是在临时位置创建它,readfile()它然后删除临时文件.
@H_301_3@请参阅以下Stack Overflow问题以获取进一步参考.它们包含一些可能适合您的解决方法:
@H_301_3@> Zip Stream in PHP
> LAMP: How to create .Zip of large files for the user on the fly,without disk/CPU thrashing
> LAMP: How to create .Zip of large files for the user on the fly,without disk/CPU thrashing