China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-08-11 23:47
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » How to use curl to get the size of a download file [Multi-threaded Download] View 1,136 Replies 2
Original Poster Posted 2010-08-20 10:20 ·  中国 广东 广州 电信
新手上路
Credits 8
Posts 8
Joined 2010-08-12 15:23
15-year member
UID 172254
Gender Male
Status Offline
I want to make a batch file for multi-threaded downloading.
Curl can download in segments and then merge them, but I don't know how curl can get the file size in advance, and then calculate how many segments it needs to be split into?
Floor 2 Posted 2010-08-20 13:18 ·  中国 北京 朝阳区 中国数码港科技有限公司BGP节点
初级用户
★★
Credits 111
Posts 62
Joined 2008-09-05 21:18
17-year member
UID 124905
Gender Male
From http://ms-dos.cz.cc
Status Offline
If the OP means PHP's curl, you can look at this code:
$file_sizeofurl =
a_array['Content-Length'];
--------------------2. Use curl to get the size of a remote file
What if the server forbids get_headers?
Use another method, with curl.
I've always felt that curl is like a virtual user, able to imitate anything.

Below is a function from a foreigner
Please note
echo '
head-->'.$head.'<----end

';
I added this line, so I could see exactly what the header contains
function
remote_filesize($uri,$user='',$pw='')
{
// start output
buffering
ob_start();
// initialize curl with given uri
$ch =
curl_init($uri);
// make sure we get the header
curl_setopt($ch,
CURLOPT_HEADER, 1);
// make it a http HEAD request
curl_setopt($ch,
CURLOPT_NOBODY, 1);
// if auth is needed, do it here
if (!empty($user)
&& !empty($pw))
{
$headers = array('Authorization: Basic ' .
base64_encode($user.':'.$pw));
curl_setopt($ch, CURLOPT_HTTPHEADER,
$headers);
}
$okay = curl_exec($ch);
curl_close($ch);
// get the
output buffer
$head = ob_get_contents();
// clean the output buffer and
return to previous
// buffer settings
ob_end_clean();

echo '
head-->'.$head.'<----end
';

// gets you the numeric value from the Content-Length
// field in the http header
$regex = '/Content-Length:s([0-9].+?)s/';
$count = preg_match($regex, $head, $matches);

// if there was a Content-Length field, its value
// will now be in $matches[1]
if (isset($matches[1]))
{
$size = $matches[1];
}
else
{
$size = 'unknown';
}
//$last=round($size/(1024*1024),3);
//return $last.' MB';
return $size;
}
20世纪下载站:http://ms-dos.cz.cc
Floor 3 Posted 2010-08-20 15:42 ·  中国 广东 广州 电信
新手上路
Credits 8
Posts 8
Joined 2010-08-12 15:23
15-year member
UID 172254
Gender Male
Status Offline
I'm using bat, not PHP, sorry.
Forum Jump: