php使用header和readfile無法下載zip文件的問題排查

項目有個需求,就是把一批二維碼桌臺號打包成zip文件後下載,在舊服務器上一直用的好好的,但最近切到k8s環境,發現不能下載了。 原來的代碼如下:

            header("Content-Type: application/zip");
            header("Content-Transfer-Encoding: Binary");
            header("Content-Length: " . filesize($zipName));
            header("Content-Disposition: attachment; filename=\"" . basename($zipName) . "\"");
            readfile($zipName);
            @unlink($zipName);

本來響應的content-type應該返回application/zip的,卻返回了application/json,後來經運維同事協助,發現原來是nginx的gzip沒有開啓導致的。 不過在不開啓gzip的情況下,還有另外一種方式也可以實現下載,代碼如下:

            header('Content-Description: File Transfer');
            header("Content-Type: application/zip");
            header("Content-Transfer-Encoding: Binary");
            header("Content-Length: " . filesize($zipName));
            header("Expires: 0");
            header("Cache-Control: must-revalidate");
            header("Content-Disposition: attachment; filename=\"" . basename($zipName) . "\"");

            ob_clean();
            flush();
            readfile($zipName);
            @unlink($zipName);
			exit;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章