Alex
Alex

Reputation: 5695

Force download of multiple files with header() in php

I'm trying to download multiple files using header() in a while loop, but only one file gets downloaded. Why?

while ($row = mysql_fetch_array($sql)) {
    header('Content-Type: text/x-vcard');
    header('Content-Disposition: attachment; filename=' . $row['name'] . '.vcf');
}

Upvotes: 1

Views: 4330

Answers (3)

Sheepy
Sheepy

Reputation: 18005

You can only transfer one file from server side at a time. Typical workarounds are:

  1. tar/zip them up into one file on server side.
  2. use javascript to window.open multiple files for download.

Upvotes: 4

Mihai Iorga
Mihai Iorga

Reputation: 39724

headers ca be set only once before outputting any data.

As you loop you set some headers after that you must output something. In the next loop you will not set any headers.

Please read php.net

Upvotes: 1

Emil Vikström
Emil Vikström

Reputation: 91963

This is not possible. The HTTP protocol does not have support for downloading multiple files. The most common workaround is to put the files in a zip archive for the client to download.

Upvotes: 1

Related Questions