Search code examples
phpphp-ziparchive

ZipArchive - zip is not getting created, but throws no errors


I'm struggling with creating zips with ZipArchive. I've modified a function that let's me create a zip from an array of file paths.

Can you spot any faults in this code, anything that prevents the zip file from being created?

// Function to create a zip archive for the low res images
function create_zip_lres($files = array(), $destination = '', $overwrite = false) {
    // If the zip file already exists and overwrite is false, return false
    if(file_exists($destination) && !$overwrite) { return false; }

    $valid_files = array();

    // If files were passed in
    if(is_array($files)) {
        foreach($files as $file) {
            // Make sure the file exists
            if(file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }

    // If we have good files
    if(count($valid_files)) {

        // Create the archive
        $zip = new ZipArchive();

        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        }

        // Add the files
        foreach($valid_files as $file) {
            $basename = basename($file);
            $zip->addFile($file, $basename);
        }

        // DEBUG
        echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;

        if ($zip->close()) {
            echo ' - Success!';
            echo '<br>';
            echo 'Your zip path is: ' . $destination;
        } else {
            echo ' - Failed!';
        }

        // DEBUG
        echo '<br>';
        print_r($zip);

        // Return filepath for zip
        return $destination;
    }
    else {
        return false;
    }
}



// getting the images array
$resize_images = resize_images();
// destination path
$lres_directory = get_lres_full_upload_dir() . get_the_title() . '-low-res.zip';

if (function_exists('create_zip_lres')) {
    create_zip_lres($resize_images, $lres_directory);
} else {

}

I've added some debugging lines to find out what's happening and although it's telling me everything looks fine, no file is created.

The zip archive contains 3 files with a status of 0 - Success!
Your zip path is: http://media.mysite.com/wp-content/uploads/lres-download-manager-files/Knus 1400-low-res.zip
ZipArchive Object ( [status] => 0 [statusSys] => 0 [numFiles] => 0 [filename] => [comment] => )

Solution

  • I have another solutions for dynamic array values enter to zip files.I have tested this is working fine.

    Hope this helps:

    <?php
    //Our Dynamic array
    $array = array( "name" => "raaaaaa",
        "test" => "/sites/chessboard.jpg"
    );
    
    //After zip files are stored in this folder
    $file= "/sites/master.zip";
    
    $zip = new ZipArchive;
    echo "zip started.\n";
    if ($zip->open($file, ZipArchive::CREATE) === TRUE) {
        foreach($array as $path){
            $zip->addFile($path, basename($path));
            echo "$path was added.\n";
        }
        $zip->close();
        echo 'done';
    } else {
        echo 'failed';
    }
    
    ?>