即时PHP ZIP文件 [英] PHP ZIP files on the fly

查看:151
本文介绍了即时PHP ZIP文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从服务器上的文件夹压缩2个文件并强制下载最简单的方法是什么?不需要将zip保存到服务器。

  $ zip = new ZipArchive(); 
//字符串file1是我们在存档中分配文件的名称
$ zip-> addFile(file_get_contents($ filepath1),'file1'); //文件1,你想压缩
$ zip-> addFile(file_get_contents($ filepath2),'file2'); //文件2,你想压缩
$ zip-> addFile(file_get_contents($ filepath3),'file3'); //文件3,你想压缩
echo $ zip-> file(); //将压缩存档发送到输出缓冲区,而不是将其写入文件。

有人可以验证:
我有一个包含test1.doc,test2.doc,和test3.doc使用上面的例子file1(file2和file3)可能只是test1.doc等。

我必须对$ filepath1做任何事吗?这是包含3个文档的文件夹目录吗?



对不起我的基本问题。

解决方案

不幸的是,在CentOS 5.x上使用PHP 5.3.4-dev和Zend Engine v2.3.0我无法获得上面的代码。我所能得到的是无效的或单元化的Zip对象错误消息。所以,为了使它工作,我不得不使用下面的代码片段(摘自Jonathan Baltazar在PHP.net手册上的示例,在ZipArchive :: open页面中):

  //准备文件
$ file = tempnam(tmp,zip);
$ zip = new ZipArchive();
$ zip-> open($ file,ZipArchive :: OVERWRITE);

//内容为
$ zip-> addFromString('file_name_within_archive.ext',$ your_string_data);
$ zip-> addFile('file_on_server.ext','second_file_name_within_archive.ext');

//关闭并发送给用户
$ zip-> close();
header('Content-Type:application / zip');
header('Content-Length:'。filesize($ file));
header('Content-Disposition:attachment; filename =file.zip');
readfile($ file);
unlink($ file);

我知道这与仅使用内存有所不同 - 除非您的内存为ram; - ) - 但也许这可以帮助别人,他们正在努力解决上述问题,就像我一样;对于哪种表现惩罚不是问题。


What's the easiest way to zip, say 2 files, from a folder on the server and force download? Without saving the "zip" to the server.

    $zip = new ZipArchive();
   //the string "file1" is the name we're assigning the file in the archive
$zip->addFile(file_get_contents($filepath1), 'file1'); //file 1 that you want compressed
$zip->addFile(file_get_contents($filepath2), 'file2'); //file 2 that you want compressed
$zip->addFile(file_get_contents($filepath3), 'file3'); //file 3 that you want compressed
echo $zip->file(); //this sends the compressed archive to the output buffer instead of writing it to a file.

Can someone verify: I have a folder with test1.doc, test2.doc, and test3.doc

with the above example - file1 (file2 and file3) might just be test1.doc, etc.

do I have to do anything with "$filepath1"? Is that the folder directory that holds the 3 docs?

Sorry for my basic question..

解决方案

Unfortunately w/ PHP 5.3.4-dev and Zend Engine v2.3.0 on CentOS 5.x I couldn't get the code above to work. An "Invalid or unitialized Zip object" error message was all I could get. So, in order to make it work, I had to use following snippet (taken from the example by Jonathan Baltazar on PHP.net manual, at the ZipArchive::open page):

// Prepare File
$file = tempnam("tmp", "zip");
$zip = new ZipArchive();
$zip->open($file, ZipArchive::OVERWRITE);

// Stuff with content
$zip->addFromString('file_name_within_archive.ext', $your_string_data);
$zip->addFile('file_on_server.ext', 'second_file_name_within_archive.ext');

// Close and send to users
$zip->close();
header('Content-Type: application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="file.zip"');
readfile($file);
unlink($file); 

I know this is different than working w/ memory only - unless you have your tmp area in ram ;-) - but maybe this can help out someone else, who's struggling with the solution above, like I was; and for which performance penalty is not an issue.

这篇关于即时PHP ZIP文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆