【问题标题】:PHP ZipArchive isn't creating a gs:// zip file in Google AppEngine, Warning: filesize(): stat failed for gs://PHP ZipArchive 未在 Google AppEngine 中创建 gs:// zip 文件,警告:filesize(): stat failed for gs://
【发布时间】:2018-12-18 22:48:18
【问题描述】:

我可以直接在 Google Storage 中写入和读取文件,但是当我尝试使用 ZipArchive 创建文件时,它失败了。 Google 表示在 GAE 中启用了 zip 扩展。

$tmpdirectory .= 'gs://#default#/tmp/user-'.$uid;
$uniqueid = uniqid() . time();
$user_visible_filename = 'export.zip';
$output_path = sprintf("%s/export.%s.zip", $tmpdirectory, $uniqueid);

$zip = new ZipArchive;
$res = $zip->open($output_path, ZipArchive::CREATE);
if ($res === true) {
    foreach ($data as $datatype => $records) {
        $filename = sprintf("%s/%s.csv", $tmpdirectory, $datatype);
        write_csv_to_filename($records, $filename);     
        $localname = basename($filename);
        $fileresult = $zip->addFromString($localname, file_get_contents($filename));
        print "adding $localname... num files in zip: ".($fileresult ? "true" : "false")." -> ".$zip->numFiles."<br/>\n";
    }
}

$closeresult = $zip->close();
print "user_visible_filename: $user_visible_filename<br/>\n";
print "zip filename: $output_path<br/>\n";
print "file size: ".filesize($output_path)."<br/>\n";

header('Content-Type: application/zip');
header('Content-Length: '.filesize($output_path));
header('Content-Disposition: attachment; filename=' . $user_visible_filename);

上面的代码写入了一些 csv 文件,我想将它们捆绑在一个 zip 文件中,并让他们的浏览器下载它。我知道上面的 headers() 不起作用,因为我正在打印它们之前的东西;我要打印这些东西来调试出了什么问题。

我能够将每个 CSV 写入 gs:// 并且我能够访问它们的正确文件大小并在写入它们后读回它们的内容。

但是,当我尝试读取 zip 文件 (gs://#default#/tmp/user-152/export.5b4565bda18481531274685.zip) 的 filesize() 时,它会出现一条大警告消息 (cannot stat file) 和堆栈跟踪,就好像该文件不存在一样。

$zip-&gt;close(); 返回 false 表示失败,但我不知道失败的原因。

【问题讨论】:

  • 您看到了什么错误?您使用的是标准版还是弹性版?
  • 另外一件事,$zip-&gt;close(); 的输出是什么?
  • @FedericoPanunzio 我得到的错误是filesize() 无法统计该文件,因为它不存在。 -&gt;close() 返回false

标签: php google-app-engine google-cloud-storage ziparchive


【解决方案1】:

我找到了一个可行的替代解决方案:TbsZip

require_once '../inc/tbszip.php';
$tmpfile = tempnam('/tmp', 'export');
$zip = new clsTbsZip();
$zip->CreateNew($tmpfile);

foreach ($data as $datatype => $records) {
    $filename = sprintf("%s/%s.csv", $tmpdirectory, $datatype);
    write_csv_to_filename($records, $filename);

    $localname = basename($filename);
    //$fileresult = $zip->addFile($filename, $localname);
    $zip->FileAdd($localname, file_get_contents($filename), TBSZIP_STRING);
}

// Flush() will send all the required headers for downloading the file.
$zip->Flush(TBSZIP_DOWNLOAD, $user_visible_filename, 'application/zip');
$zip->Close();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多