在 CakePHP 控制器中,我有生成 ZIP 的逻辑,如下所示并且工作正常,我可以在服务器上找到 ZIP 存档:
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");
非常感谢任何帮助或指导。如果我应该/可以提供更多代码 - 请询问。