【问题标题】:PHP multiple file downloadPHP多文件下载
【发布时间】:2010-09-07 22:07:39
【问题描述】:

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

你怎么能让它下载多个文件monkey.gifgirraffe.jpg

最好不要 ZIP 文件...

【问题讨论】:

标签: php file header download


【解决方案1】:

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

但是,您可以使用一些 PHP 脚本来生成多个 iframe,每个 iframe 会启动一个下载,然后以这种方式伪造它。

【讨论】:

    【解决方案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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-10
      相关资源
      最近更新 更多