请求头和响应
将1.xlsx,2.xlsx打包压缩成压缩文件至down文件夹下,压缩成功,但下载不了
前台代码:
$("#export").click(function(){ $.ajax({ url:'./index.php?act=export', type:'GET', success:function(data){ } }) });后台代码:
define('TMPL_PATH','../ZipFile/');
define('DOWN_PATH',TMPL_PATH.'down');
if($_GET['act']=='export'){
$zip = new ZipArchive();
$zipName = DOWN_PATH.'/zipFile_'.date("YmdHis").'.zip';
//生成压缩文件
if ($zip->open($zipName, ZIPARCHIVE::CREATE) === TRUE) {
//文件路径
$arr = array(TMPL_PATH.'1.xlsx',TMPL_PATH.'2.xlsx');
foreach($arr as $path){
//判断文件是否存在 basename函数使压缩文件内部路径为文件名称
if(is_file($path)){
$zip->addFile($path,basename($path));
}
}
$zip->close();
}
if(!file_exists($zipName)){
exit("无法找到文件");
}
header("Cache-Control: public");
header("Content-Description: File Transfer");
header('Content-disposition: attachment; filename='.basename($zipName)); //文件名
header("Content-Type: application/zip"); //zip格式的
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
header('Content-Length: '. filesize($zipName)); //告诉浏览器,文件大小
@readfile($zipName);
exit;
}
求各位大神指教