How to download multiple files from URL in PHP?

11,632

Solution 1

Do you want to download each file to disk? Here's how you might achieve that using CURL.

$path = '/path/to/download/directory';

foreach ($urls as $url)
{
    $ch = curl_init();

    curl_setopt($ch, CURLOPT_URL, $url);

    $fp = fopen($path . '/' . basename($url), 'w');

    // Set CURL to write to disk
    curl_setopt($ch, CURLOPT_FILE, $fp);

    // Execute download
    curl_exec ($ch);
    curl_close ($ch);

    fclose($fp);
}

Solution 2

It seems that you're not trying to download files, but rather trying to serve them. Am I correct?

The two answers provided before by Stephen Curran and Fernando have assumed that you're trying to use PHP to download and so their solutions won't work for making a browser initiate 3 separate downloads.

I'm afraid that you can't use PHP to do this, although you may still need it for the headers to force the browser to download the files instead of displaying them. You will probably have to use JavaScript on the client-side to initiate the 3 separate connections.

One idea is to keep a hidden <iframe> on the web page and then use JavaScript to point each one to an MP3 file. I think that if your frames' names are someframe1, someframe2 and someframe3, you could simply do it this way in JavaScript:

someframe1.location.href = 'path/to/one.mp3';
someframe2.location.href = 'path/to/two.mp3';
someframe3.location.href = 'path/to/three.mp3';

Keep in mind that you will still probably need PHP to make the headers.

Hope it helps.

Solution 3

HTTP doesn't directly support downloading multiple files in a single request. There's no provision for MIME-type dividors in the document body to show where one file ends and another starts. As such, all your individual files will get downloaded into a single one as specified by the last copy of the header('Content-type: attachment....') call.

Your options are to modify the script to provide links to each individual file, or do some kind of server-side archiving (.zip, .tar) and send that file instead of the individual ones.

Share:
11,632
Vishal
Author by

Vishal

Updated on June 15, 2022

Comments

  • Vishal
    Vishal almost 2 years

    I have this array.

    $urls = array(
    '0'=>'http://www.domain.com/media1.mp3'
    '1'=>'http://www.domain.com/media2.mp3'
    '2'=>'http://www.domain.com/media3.mp3'
    )
    

    I wan to download these files simultaneously with the help of PHP.

    How do I do that? Any suggestions?

    I have tried putting headers in for loop but what it does is it combines the content of all files in one big mp3 file.

    This is what I tried:

    foreach($urls as $url)
    {
    ob_start();
    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Disposition: attachment; filename=".basename($url));
    header("Content-Type: audio/mpeg");
    header("Content-Transfer-Encoding: binary");
    print file_get_contents($url);
    ob_clean();
    ob_flush();
    flush();
    sleep(1);
    }
    

    Thanks Vishal