压缩/归档文件夹使用php脚本 [英] compress/archive folder using php script

查看:172
本文介绍了压缩/归档文件夹使用php脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法可以使用php脚本压缩/归档文件夹到.zip或.rar或任何其他压缩格式,以便根据要求我们可以归档文件夹,然后提供下载链接

Is there a way to compress/archive a folder in the server using php script to .zip or .rar or to any other compressed format, so that on request we could archive the folder and then give the download link

提前感谢

推荐答案

以下是一个示例:

<?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.";
?>

这篇关于压缩/归档文件夹使用php脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆