使用 PHP 压缩文件目录

发布于 2024-07-25 11:33:42 字数 166 浏览 4 评论 0原文

我正在创建一个 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.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

裂开嘴轻声笑有多痛 2024-08-01 11:33:43

选择的答案不支持递归(子目录),所以在这里:

<?
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();
}

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();
}
冷情 2024-08-01 11:33:43

您可以使用此命令轻松地对文件夹进行 gzip:

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

此命令可以通过 phps system()-function 运行。

不要忘记 escapeshellarg() 所有命令。

You can easily gzip a folder using this command:

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

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

Don't forget to escapeshellarg() all commands.

小嗷兮 2024-08-01 11:33:43

来自@Martin Zvarík anwser。 我有一个小小的改变!
这将帮助我们将所有文件(以及子文件夹中的所有文件)压缩为 zip 文件,但不包括 root

ex:
调用时的根路径

D:\xampp\htdocs\myapp\zip-this-folder.php
D:\xampp\htdocs\myapp\index.php
D:\xampp\htdocs\myapp\assets\...
D:\xampp\htdocs\myapp\js\..

:zip-this-folder.php
它将输出 myapp.zip
myapp.zip 有

- zip-this-folder.php
- index.php
- assets\...
- js\..

zip-this-folder.php:

echo $main_path = str_replace("\\", "/", __DIR__ );// get current folder, which call scipt
$zip_file = 'myapp.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 {
                // special here: we remove root folder ("D:\xampp\htdocs\myapp\") :D
                $new_filename = str_replace($main_path."/" , "", $p);
                $zip->addFile($p, $new_filename);
                $files++;

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

    echo 'Total files: '.$files;

    $zip->close();
}

from @Martin Zvarík anwser. I had a small change!
This will help us compress all file (and all file in subfolder) to a zip file, but will not include root

ex:
root path

D:\xampp\htdocs\myapp\zip-this-folder.php
D:\xampp\htdocs\myapp\index.php
D:\xampp\htdocs\myapp\assets\...
D:\xampp\htdocs\myapp\js\..

when call: zip-this-folder.php
it will output myapp.zip
myapp.zip have

- zip-this-folder.php
- index.php
- assets\...
- js\..

zip-this-folder.php:

echo $main_path = str_replace("\\", "/", __DIR__ );// get current folder, which call scipt
$zip_file = 'myapp.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 {
                // special here: we remove root folder ("D:\xampp\htdocs\myapp\") :D
                $new_filename = str_replace($main_path."/" , "", $p);
                $zip->addFile($p, $new_filename);
                $files++;

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

    echo 'Total files: '.$files;

    $zip->close();
}
总攻大人 2024-08-01 11:33:42

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

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

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

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

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";
}
花伊自在美 2024-08-01 11:33:42
$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
$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
∞琼窗梦回ˉ 2024-08-01 11:33:42

您还可以使用这样的内容:

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

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

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 warnings about using PHP's exec() function.

琴流音 2024-08-01 11:33:42

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

只需浏览: 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...';

?>

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

注意:

  • '我的目录'==> 相对于该 PHP 脚本的目标目录;
  • '我的备份.tar.gz' ==> 将在上传此 PHP 文件的同一目录中创建;

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

Just browse: 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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文