简体   繁体   中英

PHP Create zip but exclude zip file

I am using a create zip file in PHP but I want to exclude the zip file itself.

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

This code first creates the ../../includes/updatenew.zip and then zips the whole directory, but how can I exclude the updatenew.zip from being added in the zip?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM