Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download multiple files inside foreach loop issue

Tags:

php

I have the following code to download some log files through code

$files = array( '../tmp/logs/debug.log',
                '../tmp/logs/error.log');
    foreach($files as $file) {
        header("Cache-Control: public");
        header("Content-Description: File Transfer");
        header("Content-Disposition: attachment; filename=$file");
        header("Content-Type: text/html");
        header("Content-Transfer-Encoding: binary");
        // read the file from disk
        readfile($file);
    }

But only download the first element of the array. In this case debug.log , if i swap the elements then only error.log. Any help please ?

like image 712
user1292656 Avatar asked Nov 17 '25 12:11

user1292656


1 Answers

You can only download one file per HTTP request. Effectively once the first file is sent the browser will assume that's the end of the processing and stops talking to the server.

If you want to ensure that the user downloads multiple files, one solution might be to zip them all up on-the-fly at the server-side, and then send the zip file to the user to download instead.

like image 77
ADyson Avatar answered Nov 19 '25 03:11

ADyson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!