If you really need it to go through PHP, best idea is to use streams. With streams, memory requirements are very low and they don't grow with the size of the content.
Is it your server running out of memory or does it not allow you to send the end result to the client? If it is just running out of memory, run the zipping on the file system instead. You can execute system commands in PHP with
system("...command...");
So you could, based on the user selection, aggregate the selected files in a temporary directory (using the system call to do file copying), and then zip the temporary directory (using the system call to call pkzip), and then ask PHP to send the temporary file to the client, after which your PHP script can delete it.
In this way, the zipping does not consume memory in your PHP application.
您可以在 Web 服务器/PHP 级别打开 gzip,并在 PHP 脚本中跳过压缩。 然后,只需在调用 readfile() 之间输出正确的 zip 文件头即可。
You could turn on gzip at the web server/php level and skip compression in the PHP script. Then it just becomes a matter of outputting the right zip file headers in between calls to readfile().
根据内容的动态程度以及您拥有的组合数量,您可以简单地为每个组合预先构建一个 zip 文件,然后将其返回给用户。 不过,这可能很快就会过时,因为有 5 个选项,您就必须维护 32 个不同的档案。
Depending on how dynamic the content is, and how many combinations you have, you could simply have a zip file pre-built for each combination, then just return that to the user. This could get old fast though, because with 5 options, you'd have to maintain 32 different archives.
发布评论
评论(5)
如果您确实需要它来通过 PHP,最好的想法是使用流。 对于流,内存需求非常低,并且不会随着内容的大小而增长。
If you really need it to go through PHP, best idea is to use streams. With streams, memory requirements are very low and they don't grow with the size of the content.
此线程可能会有所帮助 LAMP:如何即时为用户创建大文件的 .Zip,不会造成磁盘/CPU 抖动
This thread may help LAMP: How to create .Zip of large files for the user on the fly, without disk/CPU thrashing
是您的服务器内存不足还是不允许您将最终结果发送给客户端? 如果内存不足,请改为在文件系统上运行压缩。 您可以在 PHP 中执行系统命令,
因此您可以根据用户的选择,将选定的文件聚合到临时目录中(使用系统调用进行文件复制),然后压缩临时目录(使用系统调用来调用pkzip),然后要求 PHP 将临时文件发送到客户端,之后您的 PHP 脚本可以删除它。
这样,压缩就不会消耗 PHP 应用程序中的内存。
Is it your server running out of memory or does it not allow you to send the end result to the client? If it is just running out of memory, run the zipping on the file system instead. You can execute system commands in PHP with
So you could, based on the user selection, aggregate the selected files in a temporary directory (using the system call to do file copying), and then zip the temporary directory (using the system call to call pkzip), and then ask PHP to send the temporary file to the client, after which your PHP script can delete it.
In this way, the zipping does not consume memory in your PHP application.
您可以在 Web 服务器/PHP 级别打开 gzip,并在 PHP 脚本中跳过压缩。 然后,只需在调用
readfile()
之间输出正确的 zip 文件头即可。You could turn on gzip at the web server/php level and skip compression in the PHP script. Then it just becomes a matter of outputting the right zip file headers in between calls to
readfile()
.根据内容的动态程度以及您拥有的组合数量,您可以简单地为每个组合预先构建一个 zip 文件,然后将其返回给用户。 不过,这可能很快就会过时,因为有 5 个选项,您就必须维护 32 个不同的档案。
Depending on how dynamic the content is, and how many combinations you have, you could simply have a zip file pre-built for each combination, then just return that to the user. This could get old fast though, because with 5 options, you'd have to maintain 32 different archives.