Eri*_*ner 3 php zip gzip unzip
我正在尝试使用以下代码从目录创建zip文件,并通过http下载将其提供给用户:
// write the file
file_put_contents($path . "/index.html", $output);
// zip up the contents
chdir($path);
exec("zip -r {$course->name} ./");
$filename = "{$course->name}.zip";
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' .urlencode($filename));
header('Content-Transfer-Encoding: binary');
readfile($filename);
Run Code Online (Sandbox Code Playgroud)
我能够创建zip文件,但通过http下载它不起作用.如果我下载使用ftp客户端创建的zip文件,那么Mac的Stuffit Expander就可以解压缩文件了,但如果我通过http下载它,mac unzipper会创建一个无限循环.我的意思是说我下载的文件名为course.zip,然后解压缩文件给出course.zip.cpgz并解压缩该文件再次给出course.zip ..然后再打开.
有人有主意吗?
谢谢!
我遇到了这个问题,结果发现下载的zip文件最初插入了一个新行.
通过使用ob_clean和flush函数解决
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=".basename($archive_file_name));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($archive_file_name));
ob_clean();
flush();
echo readfile("$archive_file_name");
Run Code Online (Sandbox Code Playgroud)