Search code examples
phpiteratorphp-ziparchive

RecursiveIteratorIterator: include directory and php files in zip


I have a script which ZIPS directories, but need to add *.php files also.

Issue having is error is thrown when adding something like ../index.php.

Error produced by script below is:

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

My script:

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

I have read about RecursiveIteratorIterator at http://php.net/manual/en/class.recursiveiteratoriterator.php and also many questions here without luck in solving.

Replacing ../index.php with just ../ works, but that includes directories that do not want placed in zip.

Any input to allow insertion of php in downloaded zip much appreciated.


Solution

  • You can use FilterIterator or CallbackFilterIterator. If you use the same filters in multiple places better to use FilterIterator. For the simplicity, I use CallbackFilterIterator and define the filter function that uses preg_match to decide whether the file will be present in the loop.

    $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);
    }
    

    Take a notice of FilesystemIterator::SKIP_DOTS flag. It helps you to avoid code like this:

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

    The other approach will be to use your original code to add directories only, but for files use ZipArchive::addPattern method:

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

    Be aware, that the pattern will be matched against the file name only.