在 PHP 中从目录构建 Tar 文件,无需 exec/passthru

发布于 2024-07-09 19:19:18 字数 108 浏览 6 评论 0原文

所以我有一个客户,其当前主机不允许我通过 exec()/passthru()/ect 使用 tar,我需要定期以编程方式备份​​该站点,那么有解决方案吗?

这是一个Linux服务器。

So I have a client who's current host does not allow me to use tar via exec()/passthru()/ect and I need to backup the site periodicly and programmaticly so is there a solution?

This is a linux server.

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

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

发布评论

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

评论(4

度的依靠╰つ 2024-07-16 19:19:18

PHP 5.3 提供了一种更简单的方法来解决这个问题。

请看这里:http://www.php.net/manual/en/phardata。从目录构建.php

<?php
$phar = new PharData('project.tar');
// add all files in the project
$phar->buildFromDirectory(dirname(__FILE__) . '/project');
?>

PHP 5.3 offers a much easier way to solve this issue.

Look here: http://www.php.net/manual/en/phardata.buildfromdirectory.php

<?php
$phar = new PharData('project.tar');
// add all files in the project
$phar->buildFromDirectory(dirname(__FILE__) . '/project');
?>
抱着落日 2024-07-16 19:19:18

http://pear.php.net/package/Archive_Tar 您可以下载 PEAR tar 包并像这样使用它来创建存档:

<?php
require 'Archive/Tar.php';
$obj = new Archive_Tar('archive.tar');
$path = '/path/to/folder/';
$handle=opendir($path); 
$files = array();
while(false!==($file = readdir($handle)))
 {
    $files[] = $path . $file;
 }

if ($obj->create($files))
 {
    //Sucess
 }
else
 {
    //Fail
 }
?>

At http://pear.php.net/package/Archive_Tar you can donload the PEAR tar package and use it like this to create the archive:

<?php
require 'Archive/Tar.php';
$obj = new Archive_Tar('archive.tar');
$path = '/path/to/folder/';
$handle=opendir($path); 
$files = array();
while(false!==($file = readdir($handle)))
 {
    $files[] = $path . $file;
 }

if ($obj->create($files))
 {
    //Sucess
 }
else
 {
    //Fail
 }
?>
俏︾媚 2024-07-16 19:19:18

Archive_Tar 库。 如果由于某种原因无法使用,zip 扩展可能是另一个选择。

There is the Archive_Tar library. If that can't be used for some reason, the zip extension might be another option.

清君侧 2024-07-16 19:19:18

我需要一个可以在 Azure 网站(IIS)上运行的解决方案,并且在使用其他答案中的方法在服务器上创建新文件时遇到问题。 对我有用的解决方案是使用小型 TbsZip 库压缩,不需要将文件写入服务器中的任何位置 - 它只是通过 HTTP 直接返回。

这个线程很旧,但这种方法可能是更通用和完整的答案,所以我将代码作为替代方法发布:

// Compress all files in current directory and return via HTTP as a ZIP file
// by buli, 2013 (http://buli.waw.pl)
// requires TbsZip library from http://www.tinybutstrong.com

include_once('tbszip.php'); // load the TbsZip library
$zip = new clsTbsZip(); // instantiate the class
$zip->CreateNew(); // create a virtual new zip archive

// iterate through files, skipping directories
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'));
foreach($objects as $name => $object)
{ 
    $n = str_replace("/", "\\", substr($name, 2)); // path format
    $zip->FileAdd($n, $n, TBSZIP_FILE); // add fileto zip archive
}

$archiveName = "backup_".date('m-d-Y H:i:s').".zip"; // name of the returned file 
$zip->Flush(TBSZIP_DOWNLOAD, $archiveName); // flush the result as an HTTP download

这是整个 我的博客上的文章

I need a solution that would work on Azure websites (IIS) and had trouble with creating new files on the server using methods from other answers. The solution that worked for me was to use small TbsZip library for compression, which doesn't require to write file anywhere in the server - it's just returned directly via HTTP.

This thread is old, but this approach might be a bit more generic and complete answer, so I post the code as alternative:

// Compress all files in current directory and return via HTTP as a ZIP file
// by buli, 2013 (http://buli.waw.pl)
// requires TbsZip library from http://www.tinybutstrong.com

include_once('tbszip.php'); // load the TbsZip library
$zip = new clsTbsZip(); // instantiate the class
$zip->CreateNew(); // create a virtual new zip archive

// iterate through files, skipping directories
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator('.'));
foreach($objects as $name => $object)
{ 
    $n = str_replace("/", "\\", substr($name, 2)); // path format
    $zip->FileAdd($n, $n, TBSZIP_FILE); // add fileto zip archive
}

$archiveName = "backup_".date('m-d-Y H:i:s').".zip"; // name of the returned file 
$zip->Flush(TBSZIP_DOWNLOAD, $archiveName); // flush the result as an HTTP download

And here's the whole article on my blog.

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