http - Regarding the problem of setting content-length in PHP.
过去多啦不再A梦
过去多啦不再A梦 2017-05-16 12:58:51
0
1
1143

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?

过去多啦不再A梦
过去多啦不再A梦

reply all(1)
漂亮男人

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)echoHigh efficiency

or use X-SendFiletechnology

If 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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template