Jump to content

Downloading multiple files with cURL


Recommended Posts

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.


set_time_limit ( 0 );
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," ");


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

    foreach ($urls as $i => $url) {
    $g = $save_to . basename($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_MAXCONNECTS, 10);
    curl_setopt($conn[$i], CURLOPT_NOPROGRESS, false);
    curl_multi_add_handle ($mh,$conn[$i]);

    do {
    while ($active);
    foreach ($urls as $i => $url) {

Edited by requinix
no code tags meant auto-embedding of mp3
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.