File CakePHP2.0文件下载
我一直在寻找如何下载存储在Web服务器中的文件。然而,我没能找到我要找的东西File CakePHP2.0文件下载,file,cakephp,download,File,Cakephp,Download,我一直在寻找如何下载存储在Web服务器中的文件。然而,我没能找到我要找的东西 <h1>Resources</h1> <hr/> </div><!-- end well --> </div><!-- end col-12 --> </div><!-- end bigCallout -->
<h1>Resources</h1>
<hr/>
</div><!-- end well -->
</div><!-- end col-12 -->
</div><!-- end bigCallout -->
我已将我的文件上载到Web服务器(这正在运行)。目标路径存储在数据库中
<h1>Resources</h1>
<hr/>
</div><!-- end well -->
</div><!-- end col-12 -->
</div><!-- end bigCallout -->
我希望能够制作一个可下载文件的列表,以文件名的标题作为下载链接
<h1>Resources</h1>
<hr/>
</div><!-- end well -->
</div><!-- end col-12 -->
</div><!-- end bigCallout -->
以下是上传方法的代码,位于Staff controller下:
public function upload() {
$this->layout = 'staff';
$this->set('upload', $this->StaffUpload->find('all'));
if($this->request->is('post')){
$file = $this->request->data['Upload']['browsefile'];
$id = $this->Auth->User('usersId');
$staff = $this->Staff->find('first', array('conditions' => array('usersId' => $id)));
$dest_file = ROOT . DS . 'app' .DS. 'staff_uploads' . DS . $file['name'];
if(!is_dir(ROOT . DS . 'app' .DS. 'staff_uploads')) {
mkdir(ROOT . DS . 'app' .DS. 'staff_uploads');
}
if(move_uploaded_file($file['tmp_name'], $dest_file)) {
$this->StaffUpload->create();
$data = array(
'title' => $this->request->data['Upload']['filetitle'],
'dest' => $dest_file,
'description' => $this->request->data['Upload']['filedescription'],
'type' => $this->request->data['Upload']['uploadtype'],
'title' => $this->request->data['Upload']['filetitle'],
'staffId' => $staff['Staff']['id'],
'date_uploaded' => date('Y-m-d H:i:s')
);
if($this->StaffUpload->save($data)) {
$this->Session->setFlash('The file has been successfully uploaded!', 'alert_box', array('class' => 'alert-success'), 'uploads');
$this->redirect(array('action' => 'upload'));
}
}
}
}
<h1>Resources</h1>
<hr/>
</div><!-- end well -->
</div><!-- end col-12 -->
</div><!-- end bigCallout -->
public function resources() {
$this->layout = 'website';
}
上面的代码运行良好。现在,我想能够下载他们的标题作为他们的下载链接
<h1>Resources</h1>
<hr/>
</div><!-- end well -->
</div><!-- end col-12 -->
</div><!-- end bigCallout -->
我希望下载链接显示在此视图中,resources.ctp:
<h1>Resources</h1>
<hr/>
</div><!-- end well -->
</div><!-- end col-12 -->
</div><!-- end bigCallout -->
<h1>Resources</h1>
<hr/>
</div><!-- end well -->
</div><!-- end col-12 -->
</div><!-- end bigCallout -->
我不知道该在那个函数中放什么。我知道我还需要做另一个函数。我是CakePHP新手,非常感谢您的帮助!我没有使用媒体视图,因为它已弃用
<h1>Resources</h1>
<hr/>
</div><!-- end well -->
</div><!-- end col-12 -->
</div><!-- end bigCallout -->
谢谢大家! 您可以使用下面的功能下载媒体文件
<h1>Resources</h1>
<hr/>
</div><!-- end well -->
</div><!-- end col-12 -->
</div><!-- end bigCallout -->
public function download() {
$this->viewClass = 'Media';
// Render app/webroot/files/example.docx
$params = array(
'id' => 'example.docx',
'name' => 'example',
'extension' => 'docx',
'mimeType' => array(
'docx' => 'application/vnd.openxmlformats-officedocument' .
'.wordprocessingml.document'
),
'path' => 'files' . DS
);
$this->set($params);
}
拜访
<h1>Resources</h1>
<hr/>
</div><!-- end well -->
</div><!-- end col-12 -->
</div><!-- end bigCallout -->