php 将 zip 文件发送到浏览器/强制直接下载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7470846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:45:32  来源:igfitidea点击:

send zip file to browser / force direct download

phpzipforce-download

提问by Martin Huwa

i created with php zip ( http://php.net/manual/de/book.zip.php) a zip file

我用 php zip ( http://php.net/manual/de/book.zip.php)创建了一个 zip 文件

now i have to send it to the browser / force a download for it.

现在我必须将它发送到浏览器/强制下载它。

回答by Amber

<?php
    // or however you get the path
    $yourfile = "/path/to/some_file.zip";

    $file_name = basename($yourfile);

    header("Content-Type: application/zip");
    header("Content-Disposition: attachment; filename=$file_name");
    header("Content-Length: " . filesize($yourfile));

    readfile($yourfile);
    exit;
?>

回答by Kaivosukeltaja

Set the content-type, content-length and content-disposition headers, then output the file.

设置 content-type、content-length 和 content-disposition 标头,然后输出文件。

header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Length: '.filesize($filepath) );
readfile($filepath);

Setting Content-Disposition: attachmentwill suggest the browser to download the file instead of displaying it directly.

设置Content-Disposition: attachment将建议浏览器下载文件而不是直接显示。

回答by XDjuj

If you already have your ZIP on the server, and if this ZIP is reachable by Apache in HTTP or HTTPS, then, you should redirectto this file instead of "reading it" with PHP.

如果您已经在服务器上拥有 ZIP,并且 Apache 可以通过 HTTP 或 HTTPS 访问该 ZIP,那么您应该重定向到该文件,而不是使用 PHP “读取它”。

It's much more efficientas you don't use PHP, so no CPU or RAM are needed, and it will be faster to download, as no reading/writing by PHP needed too, only direct download. Let's Apache do the job!

由于不使用 PHP,因此效率更高,因此不需要 CPU 或 RAM,并且下载速度更快,因为也不需要 PHP 读/写,只需直接下载。让 Apache 来完成这项工作!

So a nice function could be:

所以一个不错的函数可能是:

if($is_reachable){
    $file = $relative_path . $filename; // Or $full_http_link
    header('Location: '.$file, true, 302);
}
if(!$is_reachable){
    $file = $relative_path . $filename; // Or $absolute_path.$filename
    $size = filesize($filename); // The way to avoid corrupted ZIP
    header('Content-Type: application/zip');
    header('Content-Disposition: attachment; filename=' . $filename);
    header('Content-Length: ' . $size);
    // Clean before! In order to avoid 500 error
    ob_end_clean();
    flush();
    readfile($file);
}
exit(); // Or not, depending on what you need

I hope that it will help.

我希望它会有所帮助。

回答by Roger

You need to do it this way, otherwise your zip will be corrupted:

您需要这样做,否则您的 zip 将被损坏:

$size = filesize($yourfile);
header("Content-Length: \".$size.\"");

So content-length header needs a real string, and filesize returns and integer.

所以内容长度头需要一个真正的字符串,文件大小返回和整数。