如何在php中下载到浏览器的多张图片

时间:2016-08-22 13:42:28

标签: php

我通过网络服务以这种方式获得一些图片路径:

 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'

我正在尝试通过PHP脚本将它们下载到浏览器中:

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);
        }
    }

问题是,即使我的数组包含你看到的三个链接,我也只得到一张图片。所以我做错了。

1 个答案:

答案 0 :(得分:3)

您只能发送一个文件。

您需要将zip个文件放入一个存档中,然后将该zip文件发送到浏览器。

有一些other solutions,但对于大多数情况,拉链应该是好的