2010-09-07 78 views
3

我见过的documentation for PHP readfilePHP多文件下载

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

这个例子你怎么能使它所以它下载多个文件monkey.gifgirraffe.jpg

最好不ZIP文件...

+0

一个HTTP请求,一个文件... – 2010-09-07 22:11:08

+1

见过这样一个下载地方? – 2010-09-07 22:12:03

+0

相关/可能重复:[在一个HTTP请求中下载多个文件](http://stackoverflow.com/questions/2332329/download-multiple-files-in-one-http-request) – hakre 2012-02-08 01:10:46

回答

7

你不能。这不是PHP的限制,它是HTTP/Web浏览器的限制。 HTTP不提供通过一个请求发送多个文件的机制。

但是,您可以使用一些PHP脚本生成多个iframe,每个iframe都会启动一次下载,并以此方式伪装。

+0

iframe的一个建议很酷.. ..... – effkay 2011-04-30 18:35:37

2

整个方法似乎有点没有意义,因为实际上存在于服务器上的物理文件。只需使用JavaScript打开所有的文件URL,如果你在.htaccess文件中正确设置了标题,那么这些文件就会下载。

我会做这样的事情

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