简体   繁体   中英

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/def.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.

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.

ZipArchive class is what you need. 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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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