2016-08-28 3 views
0

私はZIPSディレクトリのスクリプトを持っていますが、* .phpファイルも追加する必要があります。RecursiveIteratorIterator:zip内のディレクトリとphpファイルを含む

../index.phpのようなものを追加すると、エラーが発生します。以下のスクリプトによって生成さ

エラーは次のとおりです。

Fatal error: Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(../index.php): failed to open dir: Not a directory' in /home/mathtest/public_html/trig/admin/save.php:23 Stack trace: #0 /home/mathtest/public_html/trig/admin/save.php(23): RecursiveDirectoryIterator->__construct('../index.php') #1 {main} thrown in /home/mathtest/public_html/trig/admin/save.php on line 23 

は私のスクリプト:

<?php 

/* CONFIG */ 

$pathToAssets = array("../images", "../Data", "../css", "../index.php"); 

$filename = "temp/backup.zip"; 

/* END CONFIG */ 


$zip = new ZipArchive(); 

$zip->open($filename, ZipArchive::CREATE); 


//add folder structure 

foreach ($pathToAssets as $thePath) { 

    // Create recursive directory iterator 
    $files = new RecursiveIteratorIterator(
      new RecursiveDirectoryIterator($thePath), RecursiveIteratorIterator::LEAVES_ONLY 
    ); 


    foreach ($files as $name => $file) { 

     if ($file->getFilename() != '.' && $file->getFilename() != '..') { 

      // Get real path for current file 
      $filePath = $file->getRealPath(); 

      $temp = explode("/", $name); 

      array_shift($temp); 

      $newName = implode("/", $temp); 

      // Add current file to archive 
      $zip->addFile($filePath, $newName); 
     } 
    } 
} 

$zip->close(); 

$yourfile = $filename; 

$file_name = basename($yourfile); 

header("Content-Type: application/zip"); 
header("Content-Transfer-Encoding: Binary"); 
header("Content-Disposition: attachment; filename=$file_name"); 
header("Content-Length: " . filesize($yourfile)); 

readfile($yourfile); 

unlink('temp/backup.zip'); 

exit; 
?> 

私は解決で運なしでここにも多くの質問をhttp://php.net/manual/en/class.recursiveiteratoriterator.phpでRecursiveIteratorIteratorについて読んでてきました。

../index.phpをjust ../と置き換えるが、zipに入れたくないディレクトリも含む。

ダウンロードしたzipにPHPを挿入できるようにするための入力があれば幸いです。

+0

RecursiveDirectoryIteratorは、スキャンするディレクトリのみを使用します。したがって、ファイルを別々に扱わなければならない場合もあれば、イテレーターをインクルードファイルにも適合させることができるかもしれません。 Thriaultによるhttps://secure.php.net/manual/en/class.recursivedirectoryiterator.phpの最初のコメントは、PHPファイルを扱います。 – jedifans

+0

ありがとう@jedifans。私はそのb4の投稿を試みましたが、動作しませんでした。 zipファイルにphpファイルがないので、私のスクリプトに正しく適合していない可能性があります。 – Woody

答えて

1

FilterIteratorまたはCallbackFilterIteratorを使用できます。複数の場所で同じフィルタを使用すると、FilterIteratorを使用する方が良いでしょう。簡単にするために、私はCallbackFilterIteratorを使用し、preg_matchを使用してファイルがループ内に存在するかどうかを決定するフィルタ関数を定義します。

$path = '../test'; 

$directories = ['images', 'Data', 'css']; 

$filter = function ($current, $key, $iterator) use ($path, $directories) { 
    $path = preg_quote($path, '/'); 
    $directories = implode('|', array_map('preg_quote', $directories)); 

    if (preg_match('/^' . $path . '\/(' . $directories . ')/', $key)) { 
     return true; 
    } 

    if (preg_match('/^' . $path . '.+\.php$/', $key)) { 
     return true; 
    } 

    return false; 
}; 

$files = new CallbackFilterIterator(
    new RecursiveIteratorIterator(
     new RecursiveDirectoryIterator(
      $path, 
      FilesystemIterator::SKIP_DOTS 
     ), 
     RecursiveIteratorIterator::LEAVES_ONLY 
    ), 
    $filter 
); 

foreach ($files as $key => $file) { 
    // Do something with files. 
    var_dump($key, $file); 
} 

FilesystemIterator::SKIP_DOTSフラグに注意してください。それはあなたがこのようなコードを避けるために役立ちます。

if ($file->getFilename() != '.' && $file->getFilename() != '..') { 
    // ... 
} 

他のアプローチは、ディレクトリのみを追加するには、元のコードを使用するようになりますが、ファイルはZipArchive::addPatternメソッドを使用するために:

$zip->addPattern('/\.(?:php)$/', $path) 

は、注意してくださいをそのパターンはファイル名とのみ一致します

+0

ありがとうございます@sevavietl。これは非常に良い説明と例です、それを使用するつもり! – Woody

関連する問題