Menu

Why the download proxy streams in chunks instead of readfile
πŸ“°

Why the download proxy streams in chunks instead of readfile

DEV Community: phpΒ·Mobeen AhmedΒ·3 days ago
#RNCanxr5
#devto#dev#file#proxy#memory
Long article (741 words) β€” no AI summary yet.
Reading 0:00
15s threshold

When I first built the download proxy for ramrom.io , I did the obvious thing. PHP has a function called readfile() . You give it a URL or a path, it reads the whole file and sends it to the browser. One line, done. It worked perfectly in testing β€” until someone pasted a video that was a few hundred megabytes.

The problem is how readfile() actually works. It loads the whole file into memory before it sends anything. On a local machine with plenty of RAM, nobody notices. On a shared hosting plan, the PHP process is capped at a modest memory limit β€” mine is 128MB β€” and a single 300MB video blows straight past it. The visitor gets a blank page or a half-finished file, and the server logs a fatal error that tells me nothing useful.

So I rewrote the proxy to stream in chunks.…

Continue reading β€” create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.