$url = 'http://xxxx/1.txt';
// 获取远程文件大小函数
function remote_filesize($url)
{
ob_start();
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
$ok = curl_exec($ch);
curl_close($ch);
$head = ob_get_contents();
ob_end_clean();
$regex = '/Content-Length:\s([0-9].+?)\s/';
$count = preg_match($regex, $head, $matches);
return isset($matches[1]) ? $matches[1] : 0;
}
$len = remote_filesize($url);
set_time_limit(0);
$filename = $url;
$buffer = 1024 * 1024;//每个切片大小 单位字节
if (intval($len) < $buffer) {
$buffer = intval($len);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $filename);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$begin = 0;
$end = 0;
while ($end < intval($len)) {
$end = $begin + $buffer;
curl_setopt($ch, CURLOPT_RANGE, $begin . '-' . $end);
header('Content-Range:' . $begin . '-' . $end);
$content = curl_exec($ch);
echo $content . "<br>";
$info = curl_getinfo($ch);
$begin = $end + 1;
unset($content);
}
php使用curl下载文件
猜你喜欢
转载自blog.csdn.net/abc8125/article/details/104537245
今日推荐
周排行