【发布时间】:2015-08-03 07:46:49
【问题描述】:
我想在 php.ini 中下载 zip 文件。 下面是我的代码。
<?php
ob_start();
// set example variables
$filename = "test.zip";
$filepath = "/home/somewhere/file/zip";
// http headers for zip downloads
header("Pragma: no-cache");
header("Expires: on, 01 Jan 1970 00:00:00 GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Content-Description: File Transfer");
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=\"".$filename."\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filepath.$filename));
readfile($filepath.$filename);
?>
我的 html 代码中有一个链接。
<html>
<head>
</head>
<body>
<a href="myPHPfile.php">Download</a>
</body>
</html>
当我单击名为“Donwload”的链接时,文件已成功下载,但我无法解压缩并打开文件。
第一次下载时文件名为test.zip,只是文件名。它的扩展是在我单击它解压缩时创建的。 文件的扩展名是“.cpgz”而不是“.zip”。
这是我下载时的日志。
Resource interpreted as Document but transferred with MIME type application/zip:"myPHPfile.php"
我的代码做错了吗?还是错过了什么? 我怎样才能解决这个问题? 我上传的文件已经压缩在服务器中,我要做的就是下载它。
【问题讨论】:
-
嗨,看看这个post,可能会有所帮助。
-
ob_start();在那里做什么?
标签: php