php压缩和下载谷歌云存储图像

时间:2016-03-24 08:23:32

标签: php google-cloud-storage ziparchive

我想点击一个按钮来压缩并将图像从谷歌云存储下载到我的电脑。我使用以下代码从云存储中的mybucket下载映像:

zip文件下载成功,但是当我打开它时,显示错误。为什么文件大小只有16KB? enter image description here

我用notepad ++打开zip文件,内容是html代码,如下图所示。 enter image description here

我的完整test.php代码:

<?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>

我尝试创建另一个test2.php并使用那些普通的谷歌图片,但zip文件仍然损坏为test1.php:

我的完整test2.php代码:

<?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>

我该如何解决?

0 个答案:

没有答案