In asking around and doing some research, it seems like many people are really pushing X-Sendfile as the preferred method of serving large downloadable files to a browser-based client from a PHP script.
Some methods I've been looking into are:
Of these, most people are telling me that X-Sendfile is the best.
The problem is, I am trying to implement a download method that is not only capable of handling large files, but also widely universal on most servers, since the script I am writing will be used by many other servers than just my own.
My question is: What makes X-Sendfile so much better? If it's so good, why don't I hear a lot about it and why isn't it being used more often? Lastly, will most typical low-budget shared web hosts support X-Sendfile?
Thanks!
It allows the webserver to send the file in whatever it thinks is the best way to send that file to the client. It also enables support for range requests (i.e. download managers and resuming) as long as the webserver supports it without writing a single line of code for it.
However, "typical low-budged shared web hosts" are unlikely to support it. Consider if you really want to target them though; that usually also involves supporting outdated PHP versions, nasty configurations (safe_mode and/or magic_quotes enabled) and lack of PHP modules such as PDO (always enabled in recent PHP versions, but see one of my previous points).
Any other way of sending files to the client has the disadvantage that PHP needs to read it and then pass it to the webserver as string data (which is not efficient compared to e.g. letting the kernel write a whole file to a socket). When using file_get_contents
it is even worse - you read the whole file into memory which is not necessary at all.