首页 > 解决方案 > 使用 PHP 下载远程文件时遇到问题(空 ZIP)

问题描述

我需要从 Web 服务(GET-request)下载一个 zip 文件(通过 PHP),然后将其传输给用户进行保存。我当前的代码如下所示:

download_file('http://remotesrv/getFile?username=username&userpassword=userpassword'); //remote service, that generate a file on the fly

function download_file($file) {
    ob_clean();
    header("Expires: 0");
    header("Pragma: no-cache");
    header("Cache-Control: no-store, no-cache, must-revalidate");
    header("Cache-Control: post-check=0, pre-check=0", false);
    header("Content-Type: application/zip"); 
    header("Content-Transfer-Encoding: binary");
    header("Content-Disposition: attachment; filename=\"file.zip\"; filename*=\"file.zip\"");
    exit(readfile($file));
}

此代码在 Chrome 和 Opera 浏览器中运行良好,但在 Firefox、IE 和 Edge 中,它会下载一个空的零大小存档。告诉我一些解决问题的方法。提前谢谢,对不起我的英语:)

标签: php

解决方案


推荐阅读