代码之家  ›  专栏  ›  技术社区  ›  Mark Lalor

PHP多文件下载

  •  3
  • Mark Lalor  · 技术社区  · 15 年前

    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.gif girraffe.jpg

    最好没有 ZIP 文件夹。。。

    2 回复  |  直到 15 年前
        1
  •  9
  •   timdev    15 年前

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

        2
  •  3
  •   dokgu    7 年前

    由于服务器上实际存在一个物理文件,所以整个方法似乎有点毫无意义。只需使用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>
    
    推荐文章