Php 使用.zip archieve作为附件下载文件
我的类名MyMethods中有此函数:Php 使用.zip archieve作为附件下载文件,php,file,loops,zip,download,Php,File,Loops,Zip,Download,我的类名MyMethods中有此函数: function zipDownload($file_names,$file_path){ $zip_file_name = date('Y-m-d-h-i-s').'_attachement.zip'; $zip = new ZipArchive(); if($zip->open($zip_file_name, ZIPARCHIVE::CREATE )!==TRUE) { echo("cannot open
function zipDownload($file_names,$file_path){
$zip_file_name = date('Y-m-d-h-i-s').'_attachement.zip';
$zip = new ZipArchive();
if($zip->open($zip_file_name, ZIPARCHIVE::CREATE )!==TRUE) {
echo("cannot open <".$zip_file_name.">\n");
}
else{
$filePathAry = explode("/",$file_path);
$count = count($filePathAry);
$path = '';
for($i=0; $i<=$count - 2; $i++){
$path .= $filePathAry[$i].'/';
}
$path = $this->baseDir().$path;
foreach($file_names as $files){
$zip->addFile($path,$files);
}
$zip->close();
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=".$zip_file_name);
header("Pragma: no-cache");
header("Expires: 0");
readfile($zip_file_name);
exit;
}
}
调试时,我将此链接作为单个文件路径获取:
Path : http://localhost/sites/mySite/bdc/uploader/exhibitsFiles/06_08_2014
File Name: xyb.jpg
单击下载所有文件作为附件后,它会生成.zip文件,但不会像正常的.zip文件那样打开
我错过了什么?请帮我做这件事。谢谢。在
$zip->addFile()
中使用文件系统路径而不是http url,并确保要添加的文件存在且可读
如果必须通过http获取要添加到存档的文件,请使用以下方法:
$zip->addFromString($files, file_get_contents($path));
代替:
$zip->addFile($path,$files);
谢谢你,先生,我对代码做了一些修改,虽然与旧代码略有不同,但其思想与您在解决方案中添加的相同。这对我有用。我查过了。谢谢
$zip->addFile($path,$files);