簡體   English   中英

在沒有exec / passthru的PHP目錄中構建Tar文件

[英]Build Tar file from directory in PHP without exec/passthru

所以我有一個當前主機不允許我通過exec()/ passthru()/ ect使用tar的客戶端,我需要定期備份網站並以編程方式備份​​,所以有解決方案嗎?

這是一個linux服務器。

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');
?>

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
 }
?>

Archive_Tar庫。 如果由於某種原因無法使用,則zip擴展可能是另一種選擇。

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

這是我博客上的整篇文章

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM