Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从PHP中的目录构建Tar文件,无需exec/passthru_Php_Linux_Backup_Archive - Fatal编程技术网

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

从PHP中的目录构建Tar文件,无需exec/passthru,php,linux,backup,archive,Php,Linux,Backup,Archive,因此,我有一个客户端,它的当前主机不允许我通过exec()/passthru()/ect使用tar,我需要定期和编程地备份站点,所以有解决方案吗 这是一台linux服务器。有一个库。如果由于某种原因无法使用,则扩展可能是另一个选项。在,您可以加载PEAR tar包,并像这样使用它来创建存档: <?php require 'Archive/Tar.php'; $obj = new Archive_Tar('archive.tar'); $path = '/path/to/folder/';

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

这是一台linux服务器。

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

在,您可以加载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
 }
?>

PHP5.3提供了一种更简单的方法来解决这个问题

看这里:


我需要一个能够在Azure网站(IIS)上工作的解决方案,并且在使用其他答案中的方法在服务器上创建新文件时遇到问题。对我有效的解决方案是使用小型库进行压缩,这不需要在服务器中的任何位置写入文件——它只是通过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

总而言之。

似乎是一个奇怪的限制。但不完全是在普通主机上,许多主机只是完全阻止使用,而不是阻止用户。对,但这很愚蠢,考虑到PHP与文件系统交互的能力。我对此很好奇,因为我想要一个在Windows上也能工作的跨平台解决方案。我打字时你发了帖子,但我会把我的帖子放在这里,因为它有代码。如果你有空目录(将省略)、符号链接(省略)或断开的符号链接,这将不起作用(抛出异常),但将包括作为符号链接目标的空目录-如图所示。因此,在98%的情况下,这是一个很好的解决方案,而在其他情况下,这是一个打击自己的好方法!
// 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