3

I want to click a button to zip and download images from google cloud storage to my computer. I use the following code to download the image from mybucket in cloud storage:

The zip file is download successful, but when I open it, show error. Why the file size only 16KB? enter image description here

I open the zip file with notepad++, the content is html code as image below. enter image description here

My full test.php code:

<?php
require __DIR__ . "/includes/AutoLoader.php";

require_once 'google/appengine/api/cloud_storage/CloudStorageTools.php';
use google\appengine\api\cloud_storage\CloudStorageTools;

$downloadimg = filter_input(INPUT_POST, 'downloadimg');

if (isset($downloadimg)) {
    $valid_files = array(
        'gs://mybucket/Photos/gavesoft/Photos/20160130105829847800.jpg',
        'gs://mybucket/Photos/gavesoft/Photos/20160130102504977100.jpg'
    );

    if (count($valid_files > 0)) {
        $zip = new ZipArchive();
        $zip_name = "zipfile.zip";

        if ($zip->open($zip_name, ZIPARCHIVE::CREATE) !== TRUE) {
            echo "<script>console.log('Sorry ZIP creation failed at this time');</script>";
        }

        foreach ($valid_files as $file) {
            $zip->addFile($file);
        }

        $zip->close();

        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename=' . $zip_name);
        header('Content-Length: ' . filesize($zip_name));
        readfile($zip_name);
    }
?>

<html>
    <head></head>
    <body>
        <form action="test.php" method="post">
            <button type="submit" name="downloadimg">Download All Images</button>
        </form>
    </body>
</html>

I have tried create another test2.php and just use those normal google image, but the zip file still corrupt as test1.php:

My full test2.php code:

<?php
$downloadimg = filter_input(INPUT_POST, 'downloadimg');

if (isset($downloadimg)) {
    $valid_files = array(
        'http://google.com/images/logo.png',
        'http://upload.wikimedia.org/wikipedia/commons/thumb/5/53/Wikipedia-logo-en-big.png/220px-Wikipedia-logo-en-big.png'
    );

    if (count($valid_files > 0)) {
        $zip = new ZipArchive();
        $zip_name = "zipfile.zip";

        if ($zip->open($zip_name, ZIPARCHIVE::CREATE) !== TRUE) {
            echo "<script>console.log('Sorry ZIP creation failed at this time');</script>";
        }

        foreach ($valid_files as $file) {
            $zip->addFile($file);
        }

        $zip->close();

        header('Content-Type: application/zip');
        header('Content-disposition: attachment; filename=' . $zip_name);
        header('Content-Length: ' . filesize($zip_name));
        readfile($zip_name);
    }
?>

<html>
    <head></head>
    <body>
        <form action="test2.php" method="post">
            <button type="submit" name="downloadimg">Download All Images</button>
        </form>
    </body>
</html>

How do I solve it?

Howard Hee
  • 909
  • 1
  • 13
  • 29

0 Answers0