RecursiveIteratorIterator:在 zip 中包含目录和 php 个文件

RecursiveIteratorIterator: include directory and php files in zip

我有一个 ZIPS 目录的脚本,但还需要添加 *.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 替换为 ../ 可行,但这包括不想放在 zip 中的目录。

非常感谢任何允许在下载的 zip 中插入 php 的输入。

您可以使用 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 来决定文件是否会出现在循环中。

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

请注意,模式将仅与文件名匹配