用PHP压缩文件目录 [英] Compressing a directory of files with PHP

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

问题描述

我正在创建一个php备份脚本,该脚本将转储数据库中的所有内容并将其保存到文件中.我已经成功地做到了这一点,但是现在我需要在一个目录中拍摄数百张图像,并将它们压缩到一个简单的.tar.gz文件中.

I am creating a php backup script that will dump everything from a database and save it to a file. I have been successful in doing that but now I need to take hundreds of images in a directory and compress them into one simple .tar.gz file.

执行此操作的最佳方法是什么?如何执行?我不知道从哪里开始.

What is the best way to do this and how is it done? I have no idea where to start.

预先感谢

推荐答案

如果您使用的是PHP 5.2或更高版本,则可以使用

If you are using PHP 5.2 or later, you could use the Zip Library

然后执行以下操作:

$images_dir = '/path/to/images';
//this folder must be writeable by the server
$backup = '/path/to/backup';
$zip_file = $backup.'/backup.zip';

if ($handle = opendir($images_dir))  
{
    $zip = new ZipArchive();

    if ($zip->open($zip_file, ZipArchive::CREATE)!==TRUE) 
    {
        exit("cannot open <$zip_file>\n");
    }

    while (false !== ($file = readdir($handle))) 
    {
        $zip->addFile($images_dir.'/'.$file);
        echo "$file\n";
    }
    closedir($handle);
    echo "numfiles: " . $zip->numFiles . "\n";
    echo "status:" . $zip->status . "\n";
    $zip->close();
    echo 'Zip File:'.$zip_file . "\n";
}

这篇关于用PHP压缩文件目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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