我很好地拥有此代码及其下载文件。但是我想在从外部服务器下载文件时显示进度条。我现有的代码显示了进度条,但它没有效果(例如,如果我尝试下载两个媒体文件,一个视频和另一个音频,并且视频大小大于音频大小,音频首先完成,进度条显示100%也突然显示视频仍在下载时降至50%)。我的意思是它实际上显示了两个进展,我需要一个。如果我可以获得更好的两个进度的平均百分比值。任何有关这方面的建议将不胜感激。
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);