KubiRoazhon
KubiRoazhon

Reputation: 1839

How to download to the browser multiple pictures in php

I'm getting some pictures paths via a webservice this way :

 0 => string '/home/41/38/58/96/photos/1.jpg' 
 1 => string '/home/41/38/58/96/photos/2.jpg'
 2 => string '/home/41/38/58/96/photos/3.jpg'

I'm trying to download them to the browser via a PHP script :

foreach($files as $index => $files){
        if($index !== 'debug'){
            $file_name = basename($file);
            $final_file = @fopen($file,"rb");

            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Pragma: public");
            header("Content-Type: image/jpeg");
            header('Content-Description: File Transfer');
            header("Content-Disposition: attachment; filename=\"$file_name\"");
            header('Content-Transfer-Encoding: binary');

            header('Content-Length: '.filesize($file));

            print(@fread($final_file, filesize($file)));
            ob_flush();
            flush();

            @fclose($final_file);
        }
    }

The problem is that i'm getting just one picture even if my array contains the three links like you see. So what i'm doing wrong.

Upvotes: 1

Views: 376

Answers (1)

carmel
carmel

Reputation: 1012

you can send just one file.

You need to zip the files into one archive and then send that zip file to the browser.

there are some other solutions, but zipping should be good for most cases imho

Upvotes: 3

Related Questions