I have a 200MB file that I want to give to a user via download. However, since we want the user to only download this file once, we are doing this:
echo file_get_contents('http://some.secret.location.com/secretfolder/the_file.tar.gz');
to force a download. However, this means that the whole file has to be loaded in memory, which usually doesn't work. How can we stream this file to them, at some kb per chunk?
I found this method in http://codesamplez.com/programming/php-html5-video-streaming-tutorial
And it works very well for me
To use this class, you will have to write simple code like as below:
Use
fpassthru()
. As the name suggests, it doesn't read the entire file into memory prior to sending it, rather it outputs it straight to the client.Modified from the example in the manual:
If you would rather stream the content directly to the browser rather than a download (and if the content type is supported by the browser, such as video, audio, pdf etc) then remove the Content-Disposition header.
Take a look at the example from the manual page of
fsockopen()
:This will connect to
www.example.com
, send a request then get and echo the response in 128 byte chunks. You may want to make it more than 128 bytes.Try something like this (source http://teddy.fr/2007/11/28/how-serve-big-files-through-php/):