PHP中使用echo输出大型二进制字符串到HTTP响应时,服务器返回了一个格式错误的响应
P粉328911308
P粉328911308 2023-08-28 10:26:38
0
1
373
<p>我正在尝试使用以下代码将一些大型二进制数组输出到HTTP响应:</p> <pre class="brush:php;toolbar:false;">$bin = NULL; $strLenToOutput = 8000; for ($i=0; $i &lt; $strLenToOutput; $i++) { $bin .= pack(&quot;C&quot;, 1); } header('Content-Description: File Transfer'); header(&quot;Content-Type: application/octet-stream&quot;); header(&quot;Content-Disposition: attachment; filename=test.bin&quot;); header(&quot;Content-Length: filesize=&quot; . strlen($bin)); header(&quot;Cache-Control: no-cache&quot;); echo $bin;</pre> <p>当要输出的字符串相对较短时,小于等于8000,上述代码工作正常。但是如果字符串长度增加到8001,我会在Postman中收到以下错误:</p> <pre class="brush:php;toolbar:false;">Parse Error: The server returned a malformed response</pre> <p>我正在运行PHP7.4在Apache V2.4.46上,所有设置都是默认的。</p> <p>我在这里做错了什么?是PHP代码还是Apache2上的一些设置需要更改?</p> <p>请给予指导,提前感谢。</p> <p>更新:如果我删除设置文件长度的以下行,PHP代码将正常工作。我猜问题是我应该让PHP自己处理这部分。</p> <pre class="brush:php;toolbar:false;">//header(&quot;Content-Length: filesize=&quot; . strlen($bin));</pre></p>
P粉328911308
P粉328911308

Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!