2010-02-07 30 views
7

Tôi đã viết một hệ thống quản lý nội dung cơ bản cho trang web của mình, bao gồm bảng quản trị. Tôi hiểu tập tin cơ bản IO cũng như sao chép thông qua PHP, nhưng nỗ lực của tôi tại một kịch bản sao lưu có thể gọi được từ kịch bản đã thất bại. Tôi đã thử làm điều này:Kịch bản sao lưu đệ quy PHP

//... authentication, other functions 
for(scandir($homedir) as $buffer){ 
    if(is_dir($buffer)){ 
     //Add $buffer to an array 
    } 
    else{ 
     //Back up the file 
    } 
} 
for($founddirectories as $dir){ 
    for(scandir($dir) as $b){ 
     //Backup as above, adding to $founddirectories 
    } 
} 

Nhưng dường như nó không hoạt động.

Tôi biết rằng tôi có thể thực hiện việc này bằng FTP, nhưng tôi muốn một giải pháp hoàn toàn phía máy chủ có thể được truy cập ở bất kỳ nơi nào có đủ quyền.

+2

Đây là kỳ lạ, tôi làm việc trên chính xác cùng một vấn đề tại thời điểm này, và tôi hỏi gần như cùng một câu hỏi một phút trước :) Tôi đã xóa nó, mặc dù, bởi vì tôi đã đưa ra một câu hỏi tốt hiện có: http://stackoverflow.com/questions/1334613/zip -a-directory-in-php –

+0

Tôi ngạc nhiên không ai đề cập đến DirectoryIterator để nhận tệp của bạn. – Chris

Trả lời

9

Đây là một sự thay thế mặc dù: tại sao phải không Zip the source directory instead?

function Zip($source, $destination) 
{ 
    if (extension_loaded('zip') === true) 
    { 
     if (file_exists($source) === true) 
     { 
      $zip = new ZipArchive(); 

      if ($zip->open($destination, ZIPARCHIVE::CREATE) === true) 
      { 
       $source = realpath($source); 

       if (is_dir($source) === true) 
       { 
        $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); 

        foreach ($files as $file) 
        { 
         $file = realpath($file); 

         if (is_dir($file) === true) 
         { 
          $zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); 
         } 

         else if (is_file($file) === true) 
         { 
          $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); 
         } 
        } 
       } 

       else if (is_file($source) === true) 
       { 
        $zip->addFromString(basename($source), file_get_contents($source)); 
       } 
      } 

      return $zip->close(); 
     } 
    } 

    return false; 
} 

Bạn thậm chí có thể giải nén nó sau đó và lưu trữ cùng một hiệu ứng, mặc dù tôi phải nói rằng tôi muốn sao lưu của tôi được nén ở định dạng tệp zip.

2

Bạn có thể sử dụng đệ quy.

for(scandir($dir) as $dir_contents){ 
    if(is_dir($dir_contents)){ 
     backup($dir_contents); 
    }else{ 
     //back up the file 
    } 
} 
4

nếu bạn có quyền truy cập để thực hiện tập tin nhị phân tar qua chức năng exec nó sẽ nhanh hơn và tốt hơn tôi nghĩ rằng:

exec('tar -zcvf ' . realpath('some directory') .'/*); 

hoặc

chdir('some directory') 
exec('tar -zcvf ./*'); 
+0

chỉ là một sửa chữa đơn giản: exec ('tar -zcvf backup.gzip'. Realpath ('some directory'). '/ *'); liên kết – Michelangelo

2

bạn đã nhận nó gần như ngay

$dirs = array($homedir); 
$files = array(); 

while(count($dirs)) { 
    $dir = array_shift($dirs); 
    foreach(glob("$dir/*") as $e) 
     if(is_dir($e)) 
     $dirs[] = $e; 
     else 
     $files[] = $e; 
} 
// here $files[] contains all files from $homedir and below 

glob() là tốt hơn so với scandir() vì nhiều đầu ra phù hợp

1

tôi chúng tôi một cái gì đó gọi là UPHP. Chỉ cần gọi zip() để làm điều đó. tại đây:

<?php 
    include "uphplib.php"; 
    $folder = "data"; 
    $dest = "backup/backup.zip"; 
    zip($folder, $dest); 
?> 

UPHP là thư viện PHP. download: here

+0

liên kết không hoạt động –

0

tôi sử dụng một chức năng đơn giản để sao lưu một tập tin:

<?php 
$oldfile = 'myfile.php'; 
$newfile = 'backuped.php'; 
copy($oldfile, $newfile) or die("Unable to backup"); 

echo 'Backup is Completed'; 
?> 
Các vấn đề liên quan