簡體   English   中英

PHP ZipArchive沒有在Google AppEngine中創建gs:// zip文件,警告:filesize():gs://的統計信息失敗

[英]PHP ZipArchive isn't creating a gs:// zip file in Google AppEngine, Warning: filesize(): stat failed for gs://

我可以直接在Google存儲空間中寫入和讀取文件,但是當我嘗試使用ZipArchive創建文件時,它會失敗。 Google說zip擴展名已在GAE中啟用。

$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->close(); 返回false表示失敗,但是我不知道為什么失敗。

我找到了一個可行的替代解決方案: 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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM