由于我的数据库不会拍摄大量图像,因此我将它们直接上传到数据库。但是,我在显示它们时遇到问题,我不想下载它们,我想在页面上看到它们。我正在尝试使用以下代码显示,但它不起作用:
MyFiles 控制器的函数 display():
function display($id)
{
$file = $this->MyFile->findById($id);
$this->set('image',$file['MyFile']['data']);
}
我的文件模型:
<?php
class MyFile extends AppModel {
var $name = 'MyFile';
}
?>
MyFilesController 的函数 add()
function add() {
if (!empty($this->data) &&
is_uploaded_file($this->data['MyFile']['File']['tmp_name'])) {
$fileData = fread(fopen($this->data['MyFile']['File']['tmp_name'], "r"),
$this->data['MyFile']['File']['size']);
$this->request->data['MyFile']['name'] = $this->data['MyFile']['File']['name'];
$this->request->data['MyFile']['type'] = $this->data['MyFile']['File']['type'];
$this->request->data['MyFile']['size'] = $this->data['MyFile']['File']['size'];
$this->request->data['MyFile']['data'] = $fileData;
$this->MyFile->save($this->request->data);
$this->redirect(array('controller'=>'posts','action'=>'index'));
}
}
编辑:
MyFiles 视图的 display.ctp
<?php
echo '<img src="/MyFilesController/display/4" />';
?>