exec/passthru없이 PHP의 디렉토리에서 타르 파일 구축
문제
따라서 현재 호스트의 클라이언트가 Exec ()/Passthru ()/ECT를 통해 TAR를 사용할 수 없으며 사이트를 정기적으로 프로그래밍 방식으로 백업해야하므로 솔루션이 있습니까?
이것은 Linux 서버입니다.
해결책 2
~에 http://pear.php.net/package/archive_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
}
?>
다른 팁
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');
?>
거기에 있습니다 Archive_tar 도서관. 어떤 이유로 든 사용할 수 없다면 지퍼 확장은 또 다른 옵션 일 수 있습니다.
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
그리고 여기 전체가 있습니다 내 블로그의 기사.
제휴하지 않습니다 StackOverflow