1. Let me first talk about my usage scenario. That is, the client uses the server's interface to download files. The server side directly obtains the binary and outputs it. Is there any more reasonable return method besides echo binary?
2. Use PHP header('Content-Length: '.strlen($Download)); to set Content-Length. Is this setting actually invalid? Because logically speaking, the http request itself will set Content-Length according to the size of the output. Even if I change the settings, the final requested Content-Length is still based on the actual size.
3. Another problem is that I request the binary of a 10M file. If you request it with a browser, there will be no Content-Length header returned at all. When requesting from the client, Content-Length can be returned normally. Is the return of Content-Length also related to the request method?
Is your source a binary stream?
1. If it is a file, just to hide the real path, you can try the following method:
readfile($filename)
比echo
High efficiencyor use
X-SendFile
technologyIf it is indeed a binary stream, you can also try caching it into a file first and then
X-SendFile
2,
header
设置Content-Length
是有效的,返回真实的大小即可。这个在一些下载软件比较重要,比如迅雷。它会检测最终大小是否符合
Content-Length
3, the browser will respond with
Content-Length
为参照,但是当网络很糟糕的时候,超时、断流了,浏览器也会停止下载,最终得到的是一个不完整的文件,这个下载国外软件的时候很常见。
如果实际大小超过了
Content-Length
, and the browser will accept it.4, so even if there is no
Content-Length
return, the browser can still download. When the server disconnects, the browser will terminate the download.