使用php脚本压缩/存档文件夹

Rah*_* TS 9 php compression

有没有办法压缩/存档服务器中的文件夹使用PHP脚本到.zip或.rar或任何其他压缩格式,以便根据要求我们可以存档该文件夹,然后给出下载链接

提前致谢

Adn*_*nan 14

这是一个例子:

<?php

// Adding files to a .zip file, no zip file exists it creates a new ZIP file

// increase script timeout value
ini_set('max_execution_time', 5000);

// create object
$zip = new ZipArchive();

// open archive 
if ($zip->open('my-archive.zip', ZIPARCHIVE::CREATE) !== TRUE) {
    die ("Could not open archive");
}

// initialize an iterator
// pass it the directory to be processed
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("themes/"));

// iterate over the directory
// add each file found to the archive
foreach ($iterator as $key=>$value) {
    $zip->addFile(realpath($key), $key) or die ("ERROR: Could not add file: $key");
}

// close and save archive
$zip->close();
echo "Archive created successfully.";
?>
Run Code Online (Sandbox Code Playgroud)