header(‘Content-Type:text/html; charset= utf-8’);
$zipname='压缩文件名称';$zip = new ZipArchive;//压缩文件名 中文转gbk$filename = iconv('utf-8', 'gbk//ignore', 'Down/'.$zipname).'.zip';//1.先判断文件是否已存在//2.文件已存在的情况下,则需要删除文件重新生成//文件是否删除根据需求而定if (file_exists($filename)) {unlink($filename);}//检验压缩文件是否创建if ($zip->open($filename, \ZipArchive::OVERWRITE | \ZipArchive::CREATE)!==TRUE) {exit('无法打开文件,或者文件创建失败');}//文件路径要以绝对路径 例:http://baidu.com/1.txt 否则下载的文件大小为0,打开显示文件已损坏$file_path=file_get_contents('文件路径');$zip->addFile($file_path,'文件目录/文件名称'); //添加文件到压缩里//存在特殊符号使用$zip->addFromString( iconv('utf-8', 'gbk//ignore', '文件名称'),$file_path);//关闭$zip->close();//即使创建,仍有可能失败if(!file_exists($filename)){exit("无法找到文件");}//可以直接重定向下载//header('Location:'.$filename);//或者输出下载header("Cache-Control: public");header("Content-Description: File Transfer");header('Content-disposition: attachment; filename='.basename($filename)); //文件名header("Content-Type: application/force-download");header("Content-Transfer-Encoding: binary");header('Content-Length: '. filesize($filename)); //告诉浏览器,文件大小readfile($filename);
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号