Several lines of code can easily package and download PHP files,
The examples in this article share with you the code for packing and downloading PHP files for your reference. The details are as follows:
<? Php // obtain the file list function list_dir ($ dir) {$ result = array (); if (is_dir ($ dir) {$ file_dir = scandir ($ dir ); foreach ($ file_dir as $ file) {if ($ file = '. '| $ file = '.. ') {continue;} elseif (is_dir ($ dir. $ file) {$ result = array_merge ($ result, list_dir ($ dir. $ file. '/');} else {array_push ($ result, $ dir. $ file) ;}}return $ result;} // obtain the list $ datalist = list_dir ('.. /'); $ filename = ". /bak.zip "; // The final generated file name (including the path) If (! File_exists ($ filename) {// regenerate the file $ zip = new ZipArchive (); // You must enable zlib in linux, for windows, you need to cancel the comments if ($ zip-> open ($ filename, ZIPARCHIVE: CREATE) before php_zip.dll )! = TRUE) {exit ('file cannot be opened, or file creation failed');} foreach ($ datalist as $ val) {if (file_exists ($ val )) {$ zip-> addFile ($ val, basename ($ val); // The second parameter is the name of the file in the compressed package. If the file may be duplicated, pay attention to }}$ zip-> close (); // close} if (! File_exists ($ filename) {exit ("file not found"); // even if the file is created, it may still fail ....} Header ("Cache-Control: public"); header ("Content-Description: File Transfer"); header ('content-disposition: attachment; filename = '. basename ($ filename); // file name header ("Content-Type: application/zip"); // header in zip format ("Content-Transfer-Encoding: binary "); // tell the browser that this is the binary file header ('content-Length :'. filesize ($ filename); // tell the browser the file size @ readfile ($ filename);?>
PHP ZipArchive is a PHP extension class that can easily compress and decompress ZIP files. before using it, make sure that the php zip extension is enabled, methods for enabling PHP amplification on different platforms are available online. If you have any questions, please contact us.
Here are some common examples of File compression and decompression using php zipArchive for your reference.
I. decompress the zip file
$ Zip = new ZipArchive; // create a ZipArchive object if ($ zip-> open('test.zip ') === TRUE) {$ zip-> extraceid ('images '); // suppose you want to decompress the package to $ zip-> close () in the images folder in the current path; // close the processed zip file}
2. compress the file into a zip file
$ Zip = new ZipArchive; if ($ zip-> open('test.zip ', ZipArchive: OVERWRITE) === TRUE) {$ zip-> addFile('image.txt'); // The name of the file to be added is image.txt, in the current path $ zip-> close ();}
3. Add the content appended to the zip file
$zip=new ZipArchive;$res=$zip->open('test.zip',ZipArchive::CREATE);if($res===TRUE){ $zip->addFromString('test.txt','file content goes here'); $zip->close(); echo 'ok';}else{ echo 'failed';}
4. Package folders into zip files
Function addFileToZip ($ path, $ zip) {$ handler = opendir ($ path); // specify $ path to open the current folder. While ($ filename = readdir ($ handler ))! = False) {if ($ filename! = "." & $ Filename! = ".. ") {// The folder name is '. 'and '.. ', do not operate on them if (is_dir ($ path. "/". $ filename) {// recursive addFileToZip ($ path. "/". $ filename, $ zip);} else {// Add the file to the zip object $ zip-> addFile ($ path. "/". $ filename) ;}}@ closedir ($ path) ;}$ zip = new ZipArchive (); if ($ zip-> open('images.zip ', ZipArchive: OVERWRITE) === TRUE) {addFileToZip ('images/', $ zip); // call the method to operate the root directory to be packaged, and pass the ZipArchive object to the method $ zip-> close (); // close the processed zip file}
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.