Im create script to download file from some file server but not work perfectly.
function downloadPage2($url){
$ch = curl_init();
$userAgent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)';
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'cookie.txt');
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 8096);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($ch, $buffer) {
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s', time()) . ' GMT');
header('Cache-Control: private', false);
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="mp3.mp3"');
header('Content-Transfer-Encoding: binary');
// header('Content-Length: ' . strlen($buffer)); // provide file size
header('Connection: close');
echo $buffer;
return strlen($buffer);
});
curl_exec ($ch);
curl_close($ch);
}
And when type $url to file (mp3) from server its download this file but not right filesize. Example: File on server has size 4.5mb and my script downloaded it but size is 6mb. Music is playing but interrupts per second. Do you know where is a problem?
I tried set content-lenght in header but every function return content lengh = 0