我的方法在localhost上运行正常但在真实服务器中不起作用。它创建zip文件数据但无法下载。
<?php
function downloadBooks($id) {
$this->autoRender = false;
$id = base64_decode($id);
$this->loadModel('TeacherRegistrations');
$data = $this->TeacherRegistrations->findById($id);
$files = json_decode($data['TeacherRegistrations']['selected_handbook_id'], true);
$this->Zip->begin("files/test.zip");
foreach ($files as $key => $value) {
$folder = $this->Zip->addFile("pdf/$value.pdf", "futurekids/$value.pdf");
}
$this->viewClass = 'Media';
$params = array(
'id' => 'test.zip',
'name' => 'futurekids',
'download' => true,
'extension' => 'zip',
'path' => 'files' . DS
);
$this->set($params);
}
?>
答案 0 :(得分:1)
CakePHP 2.6具有非常简单的文件下载方式。 检查:http://book.cakephp.org/2.0/en/controllers/request-response.html#sending-files
$file_name = 'test.zip'; // File Name After Download
$file_path = 'files/test.zip'; // File Path
if (file_exists(WWW_ROOT . $file_path)) {
$this->response->file($file_path, array('download' => true, 'name' => $file_name ));
return $this->response;
}
同时检查http://php.net/manual/en/ziparchive.addfile.php
Zip-&gt; add()的第一个参数是系统中存在的文件的文件路径,而第二个参数是你想要zip的文件名。