php & cURL start giving data to browser withut waiting to read all? -
hi i'm trying downlaod file in browser via proxy script on web server. code on webserver is:
<?php $file = 'http://example.com/somefile.mp3'; download($file,2000); /* set headers total size of file loop through total size incrementing chunck size */ function download($file,$chunks){ set_time_limit(0); header('content-description: file transfer'); header('content-type: application/octet-stream'); header('content-disposition: attachment; filename='.basename($file)); header('cache-control: must-revalidate, post-check=0, pre-check=0'); header('expires: 0'); header('pragma: public'); $size = get_size($file); header('content-length: '.$size); $i = 0; while($i<=$size){ //output chunk get_chunk($file,(($i==0)?$i:$i+1),((($i+$chunks)>$size)?$size:$i+$chunks)); $i = ($i+$chunks); } } //callback function curlopt_writefunction, prints chunk function chunk($ch, $str) { print($str); return strlen($str); } //function range of bytes remote file function get_chunk($file,$start,$end){ $ch = curl_init(); curl_setopt($ch, curlopt_url, $file); curl_setopt($ch, curlopt_range, $start.'-'.$end); curl_setopt($ch, curlopt_binarytransfer, 1); curl_setopt($ch, curlopt_writefunction, 'chunk'); $result = curl_exec($ch); curl_close($ch); } //get total size of file function get_size($url){ $ch = curl_init(); curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_followlocation, true); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_header, true); curl_setopt($ch, curlopt_nobody, true); curl_exec($ch); $size = curl_getinfo($ch, curlinfo_content_length_download); return intval($size); } ?>
when try download big file (more 500mb fo example) waits long time (someimtes throws 500 error of memory limit), , after few minutes browser asks save , starts downloading.
what should start downloading in browser immediatly curl gets first chunk of file??? posted question here asnswer didn't help. still loads memory first , gives browser.
dear community i'm stucked @ 2 days. please help!!!
Comments
Post a Comment