![]() |
China DOS Union-- Unite DOS · Advance DOS · Grow DOS --Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum |
| Guest | Log in | Register | Members | Search | China DOS Union |
|
中国DOS联盟论坛 The time now is 2026-08-11 22:58 |
47,811 topics / 349,897 posts / today 0 new / 48,256 members |
| DOS批处理 & 脚本技术(批处理室) » How to use curl to get the size of a download file [Multi-threaded Download] |
| Printable Version 1,134 / 2 |
| Floor1 ybbao | Posted 2010-08-20 10:20 |
| 新手上路 Posts 8 Credits 8 | |
|
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? |
|
| Floor2 jinzihao | Posted 2010-08-20 13:18 |
| 初级用户 Posts 62 Credits 111 From http://ms-dos.cz.cc | |
|
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; } |
|
| Floor3 ybbao | Posted 2010-08-20 15:42 |
| 新手上路 Posts 8 Credits 8 | |
|
I'm using bat, not PHP, sorry.
|
|
|
[ Contact the Union admin team -
中国DOS联盟 -
Standard version ] Sponsored by ifanr Inc | © 2001–2023 |