如何使用 php 压缩文件夹并下载它?
我有名为“数据”的文件夹。该“data”文件夹包含一个文件“filecontent.txt”和另一个名为“Files”的文件夹。 “Files”文件夹包含一个“info.txt”文件。 所以它是文件夹结构中的一个文件夹。
我必须压缩这个文件夹“data”(使用php)以及其中的文件和文件夹,然后下载压缩文件。
我已经尝试过 http://www.php.net/manual/ 上提供的示例en/zip.examples.php 这些例子并没有起作用。我的 PHP 版本是 5.2.10
请帮忙。
我已经写了这段代码。
<?php
$zip = new ZipArchive;
if ($zip->open('check/test2.zip',ZIPARCHIVE::CREATE) === TRUE) {
if($zip->addEmptyDir('newDirectory')) {
echo 'Created a new directory';
} else {
echo 'Could not create directory';
}
$zipfilename="test2.zip";
$zipname="check/test2.zip";
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=check/test1.zip'); //header('Content-Length: ' . filesize( $zipfilename));
readfile($zipname); //$zip->close(); } else { echo failed';
}
?>
文件已下载但无法解压
I have folder named "data". This "data" folder contains a file "filecontent.txt" and another folder named "Files". The "Files" folder contains a "info.txt" file.
So it is a folder inside folder structure.
I have to zip this folder "data"(using php) along with the file and folder inside it, and download the zipped file.
I have tried the examples available at http://www.php.net/manual/en/zip.examples.php
These examples did not work. My PHP version is 5.2.10
Please help.
I have written this code.
<?php
$zip = new ZipArchive;
if ($zip->open('check/test2.zip',ZIPARCHIVE::CREATE) === TRUE) {
if($zip->addEmptyDir('newDirectory')) {
echo 'Created a new directory';
} else {
echo 'Could not create directory';
}
$zipfilename="test2.zip";
$zipname="check/test2.zip";
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=check/test1.zip'); //header('Content-Length: ' . filesize( $zipfilename));
readfile($zipname); //$zip->close(); } else { echo failed';
}
?>
file downloaded but could not unzip
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您需要在目录中递归添加文件。像这样的东西(未经测试):
然后调用
$zip = createZipFromDir('/tmp/dir', 'files.zip');
为了获得更多胜利,我建议阅读 SPL
DirectoryIterator
此处You need to recursively add files in the directory. Something like this (untested):
Then call
$zip = createZipFromDir('/tmp/dir', 'files.zip');
For even more win I'd recommend reading up on the SPL
DirectoryIterator
here========= 对我来说唯一的解决方案! ! !==========
放置所有子文件夹和子文件及其结构:
========= The only solution for me ! ! !==========
Puts all subfolders and sub-files with their structure:
几天前我不得不做同样的事情,这就是我所做的。
1) 检索文件/文件夹结构并填充项目数组。每个项目要么是一个文件,要么是一个文件夹,如果是文件夹,则以相同的方式将其内容检索为项目。
2) 解析该数组并生成 zip 文件。
将我的代码放在下面,您当然必须根据您的应用程序的制作方式来调整它。
用于填充和填充的方法解析:
I had to do the same thing a few days ago and here is what I did.
1) Retrieve File/Folder structure and fill an array of items. Each item is either a file or a folder, if it's a folder, retrieve its content as items the same way.
2) Parse that array and generate the zip file.
Put my code below, you will of course have to adapt it depending on how your application was made.
Methods used to fill & parse:
请参阅链接的重复项。另一个经常被忽视且特别懒惰的选择是:
See the linked duplicates. Another often overlooked and particular lazy option would be:
使用 TbsZip 类创建一个新的 zip 存档。 TbsZip 很简单,它不使用临时文件,不使用 zip EXE,没有依赖性,并且具有将存档刷新为下载文件的下载功能。
您只需在文件夹树下循环并添加存档中的所有文件,然后刷新它。
代码示例:
添加到存档中的文件将在 Flush() 方法期间按顺序压缩。所以你的档案可以包含许多子文件,这不会增加 PHP 内存。
Use the TbsZip class to create a new zip Archive. TbsZip is simple, it uses no temporary files, no zip EXE, it has no dependency, and has a Download feature that flushes the archive as a download file.
You just have to loop under the folder tree and add all files in the archive, and then flush it.
Code example:
Files added in the archive will be compressed sequentially during the Flush() method. So your archive can contain numerous sub-files, this won't increase the PHP memory.