php - How to start the download of remote big files directly? -
i'm trying pass large file external api user, (think 100mb or more)
currently, i'm using bit of paranoid script (due failures past) script downloading asap.
by 'downloading', mean download trigger on browser, not actual downloading of file. point user can select (s)he wants save file.
set_time_limit(0); apache_setenv('no-gzip', 1); ini_set('zlib.output_compression', 0); ini_set('output_buffering', 0); ini_set('implicit_flush', 1); for($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); } ob_implicit_flush(1); header('content-description: file transfer'); header('content-type: application/octet-stream'); header('content-transfer-encoding: binary'); header('content-disposition: attachment; filename="' . $filename . '"'); header('cache-control: private'); ob_flush(); flush(); $fh = fopen($external_api_url, 'rb'); while(!feof($fh)) { echo fread($fh, 512); ob_flush(); flush(); } fclose($fh);
using script, still takes 20 seconds 50mb file before download popup shows up, , longer bigger files.
is there way start stream faster?
edit: i've tried fpassthru() , readfile() these take 40 seconds same 50mb file, making me think way better. i've played around different read sizes (512, 256, 64, couple of others) didn't notice difference)
the reason taking long browser trigger download dialog, due api taking long return first chunk of data, example might reading whole file memory before starting send data you, explain why longer files take longer start though try write first 512 bytes possible.
i tried simulate locally reading local file, having sleep(5);
statement right before while
loop.
i able google chrome start download before data, omitting header('content-type: application/octet-stream');
line, while still issuing flush();
call before attempting read file. (you doing second part)
this doesn't seem work firefox 3.6, might need different gimmicks different browsers, unless can predict first character of file (take @ bom) , echo before anything, subsequently removing beginning of first fread()
call.
i hope helps! external api screwing over.
Comments
Post a Comment