我使用PHP Zip( http://php.net/manual/de/book.zip.php )创建了一个Zip文件。
现在我需要将它发送给浏览器/强制下载它。
我使用PHP Zip( http://php.net/manual/de/book.zip.php )创建了一个Zip文件。
现在我需要将它发送给浏览器/强制下载它。
<?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;
?>
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.
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="'.$filename.'"');
header('Content-Length: '.filesize($filepath) );
readfile($filepath);
设置Content-Disposition: attachment
将建议浏览器下载文件而不是直接显示它。
你需要这样做,否则你的压缩文件可能会损坏:
$size = filesize($yourfile);
header("Content-Length: \".$size.\"");