PHP创建zip但不包含zip文件

时间:2015-06-07 18:52:02

标签: php zip

我在PHP中使用创建zip文件,但我想排除zip文件本身。

$rootPath = realpath('../../');

    $zip = new ZipArchive();
    $zip->open('../../includes/updatenew.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

    $files = new RecursiveIteratorIterator(
        new RecursiveDirectoryIterator($rootPath),
        RecursiveIteratorIterator::LEAVES_ONLY
    );

    foreach ($files as $name => $file)
    {
        if (!$file->isDir())
        {
            $filePath = $file->getRealPath();
            $relativePath = substr($filePath, strlen($rootPath) + 1);

            $zip->addFile($filePath, $relativePath);
        }
    }
    $zip->close();

此代码首先创建../../includes/updatenew.zip,然后压缩整个目录,但如何将updatenew.zip排除在zip中?

1 个答案:

答案 0 :(得分:1)

update your condition from

if (!$file->isDir())

to something similar to

if (!$file->isDir() AND (strpos($name, 'updatenew.zip') === FALSE))

This will skip all files which contain 'updatenew.zip' in the name of the file.