Reputation: 91
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
Upvotes: 2
Views: 13517
Reputation: 360682
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.
Upvotes: 1
Reputation: 7433
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);
}
Upvotes: 1
Reputation: 13289
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.
Upvotes: 1
Reputation: 7757
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.
Upvotes: 0