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.
现在我必须将它发送到浏览器/强制下载它。
4 个解决方案
#1
32
<?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;
?>
#2
5
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: attachment
will suggest the browser to download the file instead of displaying it directly.
设置内容处理:附件将建议浏览器下载文件而不是直接显示它。
#3
2
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.
所以content-length头需要一个真正的字符串,filesize返回整数。
#4
2
If you already have your ZIP on the server, and if this ZIP is reachable by Apache in HTTP or HTTPS, then, you should redirect to this file instead of "reading it" with PHP.
如果您已经在服务器上安装了ZIP,并且如果Apache可以通过HTTP或HTTPS访问此ZIP,则应该重定向到此文件而不是使用PHP“读取”。
It's much more efficient as 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.
我希望它会有所帮助。
#1
32
<?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;
?>
#2
5
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: attachment
will suggest the browser to download the file instead of displaying it directly.
设置内容处理:附件将建议浏览器下载文件而不是直接显示它。
#3
2
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.
所以content-length头需要一个真正的字符串,filesize返回整数。
#4
2
If you already have your ZIP on the server, and if this ZIP is reachable by Apache in HTTP or HTTPS, then, you should redirect to this file instead of "reading it" with PHP.
如果您已经在服务器上安装了ZIP,并且如果Apache可以通过HTTP或HTTPS访问此ZIP,则应该重定向到此文件而不是使用PHP“读取”。
It's much more efficient as 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.
我希望它会有所帮助。