5

I've seen this example on the documentation for PHP readfile

<?php
$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
?>

How can you make it so It download multiple files say monkey.gif and girraffe.jpg

Preferably without ZIP files...

3
  • One HTTP Request, one file... Commented Sep 7, 2010 at 22:11
  • 1
    Ever seen such a download anywhere? Commented Sep 7, 2010 at 22:12
  • Related / Possible Duplicate: Download Multiple files in one HTTP request Commented Feb 8, 2012 at 1:10

2 Answers 2

11

You can't. It's not a PHP limitation, it's an HTTP/Web-Browser limitation. HTTP doesn't provide a mechanism for sending multiple files over one request.

You could, however, have some PHP script that generates multiple iframes, which would initiate one download each, and fake it that way.

Sign up to request clarification or add additional context in comments.

Comments

4

the whole method seems a bit pointless as a physical file actually exists on the server. just use JavaScript to open all the file urls, if you have set the header correctly in your .htaccess file then the files will just download.

I would do something like this

<script>
    var files = ['filename1.jpg', 'filename2.jpg'];
    for (var i = files.length - 1; i >= 0; i--) {
        var a = document.createElement("a");
        a.target = "_blank";
        a.download = "download";
        a.href = 'http://www.example.com/path_to/images/' + files[i];
        a.click();
    };
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.