HuyPV
Thursday, September 24, 2009
<?php
/*
$url = 'http://phungvanhuy.net/50ca/ip-log.xml';
$headers = get_headers($url,1);
$size = $headers['Content-Length'];
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RANGE, '0-' . ceil($size/2));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$content = curl_exec($curl);
echo $content;
*/
?>
<?php
set_time_limit(0);
$startTime = microtime(true);
$part = 2;
$url = 'http://cdn.amgdgt.com/base/creatives/64/18/118361/728x90.gif';
$file = 'C:\\domain\\huy.com\\cache-storage\\' . basename($url);
$headers = get_headers($url,1);
$size = $headers['Content-Length'];
$nodes = array();
$ranges = array();
for ($i = 0; $i < $part; $i++) {
$nodes[] = $url;
if ($i == 0) {$from = 0; $to = ceil($size/$part); }
elseif ($i < $part - 1) {
$from = ceil($i * $size/$part) + 1;
$to = ceil(($i+1) * $size/$part);
} elseif ($i == $part - 1) {
$from = ceil($i * $size/$part) + 1;
$to = '';
}
$ranges[] = $from . '-' . $to;
}
var_dump($ranges);
$node_count = $part;
$curl_arr = array();
$master = curl_multi_init();
for($i = 0; $i < $node_count; $i++)
{
$url =$nodes[$i];
$curl_arr[$i] = curl_init($url);
curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl_arr[$i], CURLOPT_USERAGENT, 'Firefox');
curl_setopt($curl_arr[$i], CURLOPT_RANGE, $ranges[$i]);
curl_multi_add_handle($master, $curl_arr[$i]);
}
do {
curl_multi_exec($master,$running);
} while ($running > 0);
#Combine downloaded parts
$data = '';
for($i = 0; $i < $node_count; $i++)
{
$results = curl_multi_getcontent ( $curl_arr[$i] );
$data .= $results;
//echo( $i . "\n" . htmlspecialchars($results) . "<hr />");
}
file_put_contents($file, $data);
//close the handles
for($i = 0; $i < $node_count; $i++) {
curl_multi_remove_handle($master, $curl_arr[$i]);
}
curl_multi_close($master);
//echo microtime(true) - $startTime;
header('Location: http://huy.com/cache-storage/' . basename($url));
?>
Title:
Using cURL download multi parts of one file
Description:
<?php /* $url = 'http://phungvanhuy.net/50ca/ip-log.xml'; $headers = get_headers($url,1); $size = $headers['C...
...
Rating:
4