在CakePHP 3.0中返回ZIP存档
在CakePHP控制器中,我有生成ZIP的逻辑,如下所示,工作非常好,我可以在服务器上找到ZIP存档:在CakePHP 3.0中返回ZIP存档,php,cakephp,cakephp-3.0,php-zip-archive,Php,Cakephp,Cakephp 3.0,Php Zip Archive,在CakePHP控制器中,我有生成ZIP的逻辑,如下所示,工作非常好,我可以在服务器上找到ZIP存档: function getAttachmentsInZip( $meetingId = null ){ $this->autoRender = false; $this->request->allowMethod( ['post'] ); $allTasksWithFiles_query = $this->Tasks->find('all')
function getAttachmentsInZip( $meetingId = null ){
$this->autoRender = false;
$this->request->allowMethod( ['post'] );
$allTasksWithFiles_query = $this->Tasks->find('all')
->where(['Tasks.uploaded_file_path != ' => 'NULL']);
$allTasksWithFiles = $allTasksWithFiles_query->toArray();
$files = array();
foreach( $allTasksWithFiles as $taskWithFile ){
$files[] = $taskWithFile['uploaded_file_path'];
}
if( !empty( $files ) ){
$destination = 'uploads/archives/meeting_' . $meetingId .'.zip';
$zip = new ZipArchive();
$zip->open( $destination, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE );
foreach( $files as $file ){
$zip->addFile('uploads/uploaded_files/' . $file, $file);
}
$zip->close();
}
}
但是,我完全无法将存档直接返回到用户的浏览器。我得到的最接近的是以下片段,但存档已损坏,无法打开:
header("Content-type: application/zip");
header('Content-Disposition: attachment; filename="' . $destination . '"');
header("Pragma: no-cache");
header("Expires: 0");
readfile("asd");
非常感谢您的帮助或指导。如果我应该/本可以提供更多代码,请询问。不要手动使用标题,请使用响应对象方法设置标题。请参阅并遵循。非常感谢,这确实提供了帮助,我成功地完成了工作。不要手动使用标题,请使用响应对象方法设置标题。谢谢,这确实帮了我的忙,我成功地完成了工作。