简体   繁体   English

使用PHP将多个文件和文件夹合并为一个文件

[英]Combine multiple files and folders into one file using PHP

I have designed an online editor with which a user can create files and folders on the server. 我设计了一个在线编辑器,用户可以使用该编辑器在服务器上创建文件和文件夹。 Suppose a user creates the below files and folders. 假设用户创建以下文件和文件夹。

/docs/abc.txt /docs/abc.txt
/docs/def.txt /docs/def.txt
/docs/work/assignment.txt /docs/work/assignment.txt

I want to write a PHP script to combine the files into a single ZIP file so that it makes it easier for the user to download them with one click. 我想编写一个PHP脚本,将文件组合成一个ZIP文件,以便用户轻松单击即可下载它们。

The only purpose is that I should get a single file with the directory hierarchy maintained which the user can download and uncompress on his system to get the original files and directory structure back. 唯一的目的是使我得到一个维护了目录层次结构的文件,用户可以在其系统上下载并解压缩该文件,以恢复原始文件和目录结构。 I don't care even if the ZIP file is not compressed. 即使压缩文件没有压缩,我也不在乎。

Note that I am using a shared server and do not have access to execute external commands. 请注意,我正在使用共享服务器,并且无权执行外部命令。

ZipArchive class is what you need. 您需要ZipArchive类。 Using this you can add directories and file to an archive. 使用此功能,您可以将目录和文件添加到存档中。

This is a common requirement and has been solved. 这是一个普遍的要求,并且已经解决。 Try and check out this class: http://www.phpclasses.org/browse/package/2322.html 尝试检查此类: http : //www.phpclasses.org/browse/package/2322.html

More tutorials at: http://davidwalsh.name/create-zip-php <-- this has a similar example if you scroll down http://www.w3schools.com/php/php_ref_zip.asp 更多教程,请访问: http : //davidwalsh.name/create-zip-php <-如果您向下滚动http://www.w3schools.com/php/php_ref_zip.asp,也会有类似的示例

假设您具有执行外部命令的权限,则可以使用exec运行具有外部命令行功能的ZIP程序,例如7-Zip

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM