doudi4014 2015-01-18 08:53
浏览 82

在php中使用cURL下载多个文件

I have this code and its downloading file nicely. But I want to show a progress bar while downloading file from external server. My existing code shows progress bar but its not effective (ex. if i try to download two media file, one video and another audio, and video size is larger than audio size the audio finishes first and the progress bar shows 100% also suddenly it drops to 50% as the video is still downloading ). I mean it actually shows two progress and I need one. If I could get average percentage value of two progress that would be better. Any suggestion regarding this will be greatly appreciated.

function define_progress_callback($i) {
global $conn;

curl_setopt($conn[$i], CURLOPT_PROGRESSFUNCTION, function ($resource,$download_size, $downloaded, $upload_size, $uploaded)
{
if($download_size > 0)
    $progress = round($downloaded / $download_size  * 100);
    echo '<script language="javascript">$(".loader").loader("setProgress", '.$progress.');</script>';
    echo str_pad("",1024," ");
    flush();
    usleep(20000);

});
}

$urls = array("http:/example.com/file.mp3",
              "http:/example.com/file.mp4");
$save_to='./tmp/';
$conn = array();
$fp = array();
$mh = curl_multi_init();

foreach ($urls as $i => $url) {
$g = $save_to . basename($url);
$conn[$i]=curl_init($url);
$fp[$i]=fopen ($g, "wb");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);    // No certificate
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt ($conn[$i], CURLOPT_FILE, $fp[$i]);
curl_setopt ($conn[$i], CURLOPT_HEADER ,0);
curl_setopt($conn[$i],CURLOPT_CONNECTTIMEOUT,60);
curl_setopt ($conn[$i], CURLOPT_MAXCONNECTS, 10);
curl_setopt($conn[$i], CURLOPT_NOPROGRESS, false);
define_progress_callback($i);
curl_multi_add_handle ($mh,$conn[$i]);
}

do {
$n=curl_multi_exec($mh,$active);
}
while ($active);
foreach ($urls as $i => $url) {
curl_multi_remove_handle($mh,$conn[$i]);
curl_close($conn[$i]);
fclose($fp[$i]);
}
curl_multi_close($mh);
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 matlab生成电测深三层曲线模型代码
    • ¥50 随机森林与房贷信用风险模型
    • ¥50 buildozer打包kivy app失败
    • ¥30 在vs2022里运行python代码
    • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
    • ¥15 求解 yolo算法问题
    • ¥15 虚拟机打包apk出现错误
    • ¥15 用visual studi code完成html页面
    • ¥15 聚类分析或者python进行数据分析
    • ¥15 三菱伺服电机按启动按钮有使能但不动作