开发者

PHP multiple file download

开发者 https://www.devze.com 2023-01-15 19:08 出处:网络
I\'ve seen this example on the documentation for PHP readfile <?php $file = \'monkey.gif\'; if (file_exists($file)) {

I've seen this example on the 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;
}
?>

How can you make it so It download multiple files say monkey.gif and girraffe.jpg

Preferably without ZIP file开发者_如何学Pythons...


You can't. It's not a PHP limitation, it's an HTTP/Web-Browser limitation. HTTP doesn't provide a mechanism for sending multiple files over one request.

You could, however, have some PHP script that generates multiple iframes, which would initiate one download each, and fake it that way.


the whole method seems a bit pointless as a physical file actually exists on the server. just use JavaScript to open all the file urls, if you have set the header correctly in your .htaccess file then the files will just download.

I would do something like this

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

精彩评论

暂无评论...
验证码 换一张
取 消