Last active
August 29, 2015 14:04
-
-
Save MattSandy/1113347a896cb3b6b0a0 to your computer and use it in GitHub Desktop.
Threaded Downloads with Curl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function thread_download($file, $filename) | |
{ | |
$url = $file; | |
$headers = get_headers($url,1); | |
//print_r($headers); | |
$bytes = $headers['Content-Length'][1]; | |
//echo $bytes . "|"; | |
for($i=0;$i<$bytes-1;$i+=204800) | |
{ | |
if($i+204800-1<=$bytes-1) | |
{ | |
$ch[count($ch)] = $i . '-' . ($i+204800-1); | |
} | |
else | |
{ | |
$ch[count($ch)] = $i . '-' . ($bytes-1); | |
} | |
} | |
//print_r($ch); | |
//die(); | |
//create the multiple cURL handle | |
$mh = curl_multi_init(); | |
foreach($ch as $name=>$range) | |
{ | |
$fp[$name] = fopen($filename . '.part' . $name, 'w'); | |
// create both cURL resources | |
$ch[$name] = curl_init(); | |
// set URL and other appropriate options | |
curl_setopt($ch[$name], CURLOPT_URL, $headers['Location']); | |
//curl_setopt($ch[$name], CURLOPT_FOLLOWLOCATION, true); | |
curl_setopt($ch[$name], CURLOPT_MAXREDIRS, 10); | |
curl_setopt($ch[$name], CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.7) Gecko/20091221 Firefox/3.5.7"); | |
curl_setopt($ch[$name], CURLOPT_HEADER, false); | |
curl_setopt($ch[$name], CURLOPT_RETURNTRANSFER, true); | |
curl_setopt($ch[$name], CURLOPT_RANGE, $range); | |
curl_setopt($ch[$name], CURLOPT_FILE, $fp[$name]); | |
curl_multi_add_handle($mh,$ch[$name]); | |
} | |
$running=null; | |
//execute the handles | |
do { | |
curl_multi_exec($mh,$running); | |
} while ($running > 0); | |
//close the handles | |
foreach($ch as $name=>$range) | |
{ | |
curl_multi_remove_handle($mh,$ch[$name]); | |
fclose($fp[$name]); | |
} | |
curl_multi_close($mh); | |
//------------------------------------------------------------- | |
unlink($filename); | |
$fp = fopen($filename, 'a'); | |
foreach($ch as $name=>$url) | |
{ | |
$contents = file_get_contents($filename . '.part' . $name); | |
unlink($filename . '.part' . $name); | |
fwrite($fp, $contents); | |
} | |
fclose($fp); | |
return true; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment