views:

125

answers:

4

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

A: 

Check out cURL:

PHP supports libcurl, a library created by Daniel Stenberg, that allows you to connect and communicate to many different types of servers with many different types of protocols. libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols. libcurl also supports HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading (this can also be done with PHP's ftp extension), HTTP form based upload, proxies, cookies, and user+password authentication.

You can go through the example and use a similar code inside your for loop.

Fernando
I tried cURL too! It doesn't work. I tried this example too.http://www.askapache.com/php/curl-multi-downloads.html#menu0-el1but no joy!
Vishal
A: 

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);
}
Stephen Curran
Let me try that now
Vishal
It just prints everything on the browser! and says permission denied for fopen. When we do fopen, where is it going to write the file? I tried my c:/ windows folder but it doesn't work :(
Vishal
It will write the file to the current working directory. It might be better in your case to specify an absolute path to a directory and make sure that PHP has permissions to write to this directory. I'll modify my example to reflect that.
Stephen Curran
I'm confused. Are you trying to do download the file or serve it (or both)?
Stephen Curran
A: 

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.

Helgi Hrafn Gunnarsson
Hi Helgi, Thanks for your reply. I actually want to download those files on my computer. Is your solution going to work there too?
Vishal
Whoa, I'm confused.Why are you sending headers and printing out the contents of the files if you want to download them?
Helgi Hrafn Gunnarsson
than how do I download them? I am under impression that we need to change headers to download file! Like for example if I want to download CSV file and If I change the header than it will straight away starts downloading the file.
Vishal
A: 

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.

Marc B
I do have links to each individual file. What can be done with that? It's already in the array.
Vishal
and to zip all the files, i still need to get contents of each file and write them onto server and the zip those files and download it....
Vishal