PHP强制下载问题(服务器配置问题)

时间:2014-02-15 08:15:16

标签: php force-download

我创建了一个功能,一旦用户点击链接就发出下载,该文件位于第三方存储服务(Sugar Sync)中,并通过其REST API访问。现在我已经创建了强制下载功能,并测试它在localhost上运行正常(提示下载对话框),但是当我在服务器上运行该函数时,它返回错误页面'找不到文件'。我认为这可能是需要在服务器端设置的一些PHP配置,但我不知道哪个,所以非常感谢任何帮助或提示。

以下是代码片段:

$sugarsync = new SugarSync($refreshtoken);

$response = $sugarsync->get($url); 
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: no-cache, must-revalidate");
header("Content-Type: ".$response->mediaType);
header("Content-Type: application/force-download");
header("Content-Disposition: attachment; filename=".$response->displayName.";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$response->size);

//file is returned as binary data from the API
print($sugarsync->download(urldecode($url)));
exit();

2 个答案:

答案 0 :(得分:0)

事实证明,经过进一步的故障排除后,问题与输出缓冲有关,所以我只需要在服务器配置上启用它。

答案 1 :(得分:0)

尝试在打印功能之前添加ob_get_clean();,如下所示:

ob_get_clean();
//file is returned as binary data from the API
print($sugarsync->download(urldecode($url)));
exit();