1

I am making a call(PHP server) to an API and I can confirm that I am getting a response, my code at the point where I know I am getting a response is:

$binaryF = $rsObject->makeCall('get', "/JobData/{$_GET["jID"]}", "?format=bin");

//header("Pragma: public"); // required
//header("Content-Type: application/zip");
//header("Content-Disposition: attachment; filename=ss.zip");
//header("Content-Length: " . filesize($binaryF.length));
//header("Content-Transfer-Encoding: binary");

file_put_contents('C:\ss.zip', $binaryF);

If I keep the code as is and click on the link that takes me to the page with this code on it, ss.zip is created and I open it and confirm that it has the correct content. The data is coming from an API call on the first line and is basically a zip package. If I remove the comments and comment out the file_put_contents line then the browser opens a file save dialog box but if I save it the archive is 0 bytes?

How do I send the content to a browser after retrieving it from the api call? I do not want to save it to disk first, I want to send it to the browser making the request directly.

Thank you Jack

Ok. I changed it to:

header("Pragma: public"); // required
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=ss.zip");
header("Content-Length: " . strlen($binaryF));
header("Content-Transfer-Encoding: binary");

and now it opens the file save. I save it and it is 8 K but when I try t oopen it I get a message of: "Windows cannot open the folder" It complains that the zip is invalid? At least I am getting somewhere, thanks for the help so far!

4

4 回答 4

0

尝试(未经测试):

header('Content-type: application/octet-stream');
header('Content-disposition: attachment; filename=ss.zip');

echo $binaryF;
于 2012-11-02T23:54:02.270 回答
0

$binaryF需要是您尝试提供的文件的文件路径的字符串值filesize()才能工作。

您可以使用strlen()获取文件大小以在 ContentLength 标头中使用。

当然,您仍然需要将文件实际输出到客户端浏览器。

于 2012-11-02T23:55:19.807 回答
0

好的,@zerkms 帮助我指出我应该使用: strlen($binaryF) 而不是 filesize($binaryF.length) 他在聊天会话中通过告诉我打开 zip 存档(使用像记事本这样简单的东西)帮助我我确实得到了 file_put_contents('C:\ss.zip', $binaryF); 这表明生成的内容只是部分正确,有一些 HTML 内容滑入了实际调用的响应中。所以所有的功劳都归于@zerkms,谢谢。

什么有效?以下对我有用:

header("Pragma: public"); // required
header("Content-Type: application/zip");
header("Content-Disposition: attachment; filename=ss.zip");
header("Content-Length: " . filesize($binaryF.length));
header("Content-Transfer-Encoding: binary");

echo $binaryF
于 2012-11-05T00:33:24.380 回答
0

我对 PDF 有一些类似的问题,对我来说Content-Length无法正确计算,strlen()因为它不支持multibyte-characters.

试试这个:

header("Content-Length: " . mb_strlen($binaryF));

有关更多详细信息,请参阅文档:http ://www.php.net/manual/en/function.mb-strlen.php

于 2012-11-03T00:15:00.263 回答