When I built the download proxy for ramrom.io, my first version had a bug that only showed up on long videos. Short clips downloaded fine. Then someone tried a ten-minute video and the download died halfway through with a blank page. The server had run out of memory.
The problem was embarrassingly simple. My code fetched the whole video file into a PHP variable, then sent it out with echo . For a 50 MB clip that worked. For a 300 MB video it did not, because the server only gives PHP a fixed amount of memory β on my shared hosting, 128 MB. The file itself was bigger than the memory I was allowed to use. PHP killed the script, the visitor got nothing, and my error log filled up.
The fix is to stop holding the whole file at once and stream it instead. Think of it like pouring water through a hose rather than filling a bucket and tipping it out. You read a small chunk from the source, write that chunk straight to the visitor, and repeat until the file is done.β¦