我需要使用codeigniter从我的数据库下载多个图像文件作为zip格式。我尝试了很多方法,没有什么能帮助我。它确实下载了文件,但有时我尝试打开下载的文件,我收到一个错误。它似乎文件已损坏..有人可以告诉我这些代码有什么问题吗?
function download(){
$pid = $this->input->get('id');
$bookviews = $this->Insert_model->singledownload($pid);
$files = array();
for($i=1;$i<=5;$i++){
$imgpath=$bookviews['image'.$i];
if($imgpath){
array_push($files, $imgpath);
}
}
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($files as $file) {
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename='.$zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
}