使用 PHP 压缩文件目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1048805/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:46:22  来源:igfitidea点击:

Compressing a directory of files with PHP

phpcompressionbackup

提问by VinkoCM

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.

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

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

什么是最好的方法来做到这一点,它是如何完成的?我不知道从哪里开始。

Thanks in advance

提前致谢

采纳答案by Paul Dixon

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

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

and then do something along the lines of:

然后按照以下方式做一些事情:

$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";
}

回答by SergO

$archive_name = 'path\to\archive\arch1.tar';
$dir_path = 'path\to\dir';

$archive = new PharData($archive_name);
$archive->buildFromDirectory($dir_path); // make path\to\archive\arch1.tar
$archive->compress(Phar::GZ); // make path\to\archive\arch1.tar.gz
unlink($archive_name); // deleting path\to\archive\arch1.tar

回答by geerlingguy

You can also use something like this:

你也可以使用这样的东西:

exec('tar -czf backup.tar.gz /path/to/dir-to-be-backed-up/');

Be sure to heed the warningsabout using PHP's exec()function.

请务必注意有关使用 PHPexec()函数的警告

回答by Reza Mamun

My simple 2 lines PHP file by which I can create a backup ZIP file within a few seconds:

我的简单 2 行 PHP 文件,通过它我可以在几秒钟内创建一个备份 ZIP 文件:

Just browse: http://mysiteurl.com/create-my-zip.php

只需浏览:http: //mysiteurl.com/create-my-zip.php

<?php
/* PHP FILE NAME: create-my-zip.php */

echo exec('tar zcf my-backup.tar.gz mydirectory/*');

echo '...Done...';

?>

Simply upload this file into the server and browse directly.

只需将此文件上传到服务器并直接浏览即可。

Notes:

笔记:

  • 'mydirectory' ==> target directory relative to this PHP script;
  • 'my-backup.tar.gz' ==> will be created at the same directory where this PHP file is uploaded;
  • 'mydirectory' ==> 相对于这个 PHP 脚本的目标目录;
  • 'my-backup.tar.gz' ==> 将在此 PHP 文件上传的同一目录中创建;

回答by Martin Zvarík

Chosen answer doesn't support recursion (subdirectories), so here you go:

选择的答案不支持递归(子目录),所以你去:

<?
ignore_user_abort(true);
set_time_limit(3600);


$main_path = 'folder2zip';
$zip_file = 'zipped.zip';

if (file_exists($main_path) && is_dir($main_path))  
{
    $zip = new ZipArchive();

    if (file_exists($zip_file)) {
        unlink($zip_file); // truncate ZIP
    }
    if ($zip->open($zip_file, ZIPARCHIVE::CREATE)!==TRUE) {
        die("cannot open <$zip_file>\n");
    }

    $files = 0;
    $paths = array($main_path);
    while (list(, $path) = each($paths))
    {
        foreach (glob($path.'/*') as $p)
        {
            if (is_dir($p)) {
                $paths[] = $p;
            } else {
                $zip->addFile($p);
                $files++;

                echo $p."<br>\n";
            }
        }
    }

    echo 'Total files: '.$files;

    $zip->close();
}

回答by alexn

You can easily gzip a folder using this command:

您可以使用以下命令轻松 gzip 文件夹:

tar -cvzpf backup.tar.gz /path/to/folder

This command can be ran through phps system()-function.

该命令可以通过 phps system() 函数运行。

Don't forget to escapeshellarg() all commands.

不要忘记转义shellarg() 所有命令。