在没有exec/passthru的PHP目录中构建Tar文件

Unk*_*ech 10 php linux backup archive

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

这是一个linux服务器.

小智 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');
?>
Run Code Online (Sandbox Code Playgroud)


Unk*_*ech 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
 }
?>
Run Code Online (Sandbox Code Playgroud)