10

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

这是一个linux服务器。

4

4 回答 4

19

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

看这里: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');
?>
于 2011-07-06T19:35:23.160 回答
8

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
 }
?>
于 2008-12-02T05:37:18.640 回答
4

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

于 2008-12-02T05:34:39.457 回答
0

我需要一个可以在 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

这是我博客上的整篇文章

于 2013-04-22T20:52:50.867 回答