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

╄→гoц情女王★ 提交于 2019-12-11 04:32:40

问题


I'm getting a "bad gateway error 502" when trying to download a server-generated .zip-file

I've isolated the line that causes the error; it's:

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

Here's my code. My script loops through all the .jpg files users have uploaded, bundles them into one big .zip file, and sends download headers for the .zip file.

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();

}

I have absolutely no experience with "bad gateway errors", your help is very much appreciated!

Thanks, Matt


回答1:


I've solved the problem.

The reason was generating the zip file took more than 5 seconds, and my gateway server had a Timeout and KeepAliveTimeout setting of 5 seconds. (you can edit that in httpd.conf)

I increased the Timeout to 60 seconds - problem solved.



来源:https://stackoverflow.com/questions/15864832/bad-gateway-error-502-when-trying-to-download-server-generated-zip-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!