Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP ZipArchive cannot destroy the zip context

Tags:

php

zip

While trying to zip the contents of a folder in my host to migrate the data easier (VPS, no fancy interface panels), the script throws me an error I cannot decode. I can always zip the content from command line and work with it but I would like to create a simple interface for my client to download in one click my work at anytime. Here's the error :

Warning: Unknown: Cannot destroy the zip context in Unknown on line 0

The script :

define('DS', DIRECTORY_SEPARATOR);
define('DR', realpath(dirname(__FILE__)).DS);

if (!isset($_GET['folder']))
{
    die('folder missing');
}

$rootPath = DR.$_GET['folder'].DS;

// Initialize archive object
$zip = new ZipArchive();
$zip->open(DR.'backup'.DS.'backup.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);

// Create recursive directory iterator
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($rootPath),
    RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file)
{
    // Skip directories (they would be added automatically)
    if (!$file->isDir())
    {
        // Get real and relative path for current file
        $filePath = $file->getRealPath();
        $relativePath = substr($filePath, strlen($rootPath) + 1);

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

// Zip archive will be created only after closing object
$zip->close();

Any guesses?

like image 340
yoda Avatar asked Feb 10 '23 02:02

yoda


1 Answers

I just solved the same error message in one of my own project... No idea if it's the same issue, but in my case I had not created the folder that the zip was being added to, so it would throw that error. Once I added the folder everything worked fine. Hope that helps.

like image 174
AlexW.H.B. Avatar answered Feb 12 '23 17:02

AlexW.H.B.