PHP multiple file download

10,800

Solution 1

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.

Solution 2

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>
Share:
10,800
Mark Lalor
Author by

Mark Lalor

I began my programming journey at the age of 11. My 5th grade teacher showed me that I could save a file on notepad with another file extension than .txt! Thus began my interest in HTML, CSS, Javascript, PHP, PHP GD, jQuery, SQL, C#, .NET Framework, C, C++, mobile apps, DragonFireSDK (7/10 would not use again), Objective C, and Java, in that order. I learned only through books and the internet, which is why I asked many dumb questions years ago. I like to look back on them and reminisce on my bad programming skills and problem-solving.

Updated on June 07, 2022

Comments

  • Mark Lalor
    Mark Lalor almost 2 years

    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...