网关错误502;尝试下载服务器生成的.zip文件时


"Bad Gateway Error 502" when trying to download server-generated .zip file

尝试下载服务器生成的。zip-file

时,出现"网关错误502"

我已经隔离了导致错误的行;它是:

$zip->addFile($thumb->loadVersion($version['id']), $rank . "_" . $thumb->filename);

这是我的代码。我的脚本遍历用户上传的所有.jpg文件,将它们捆绑成一个大的.zip文件,并发送.zip文件的下载头。

function downloadGalleryZip() {
    $tmpfile = tempnam("tmp", "zip");
    $zip = new ZipArchive();
    $res = $zip->open($tmpfile, ZipArchive::OVERWRITE);
    if ($res === TRUE) {
    // loop through gallery
    $sql = "SELECT * FROM files_versions WHERE file_id = ". $this->fileId . " AND deleted_on IS NULL ORDER BY rank ASC"; 
    $result = db_query($sql);
    $rank = 0;
        while ($version = mysql_fetch_array($result)) :
            $rank ++;
            $rank = str_pad($rank, 3, '0', STR_PAD_LEFT);
            $thumb = new fancyFile();
            $zip->addFile($thumb->loadVersion($version['id']), $rank . "_" . $thumb->filename);
        endwhile;  
        // $zip->addFromString('000_info.txt', 'Dieser Ordner wurde am ' . date("d.m.Y", time()) . ' heruntergeladen.');
        $zip->close();
    }

    $filename = getCaptionFromFile($this->fileId) . ".zip";
    header("Connection: Keep-Alive");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=" . $filename);
    header("Content-Type: application/zip" );
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length:' . filesize($tmpfile));
    ob_clean();
    flush();

}

我绝对没有"坏网关错误"的经验,非常感谢您的帮助!

谢谢,马特

我已经解决了这个问题。

原因是生成zip文件花费了超过5秒,并且我的网关服务器的Timeout和KeepAliveTimeout设置为5秒。(你可以在httpd.conf中编辑)

我将超时时间增加到60秒-问题解决。