我正在尝试使用cakephp和mysql来显示图像。我的代码正在运行,但它没有显示图像 uploadsController是::
<?php
class UploadsController extends AppController{
var $name='Uploads';
function share_video($stu_id=null)
{
$this->set('stu',$stu_id);
$tut=$this->Session->read('tutor_id');
$data=$this->Upload->query("select * from tutors AS Tutor where tutor_id='$tut'");
$this->set('val1',$data);
if (!empty($this->data)) {
$this->Upload->create();
if ($this->uploadFile() && $this->Upload->save($this->data)) {
$this->Session->setFlash(__('The upload has been saved', true));
//$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The upload could not be saved. Please, try again.', true));
}
}
$data=$this->Upload->find('all');
$this->set('val2',$data);
$this->layout='tutor';
}
function uploadFile() {
$id = mysql_insert_id();
$file = $this->data['Upload']['file'];
if ($file['error'] === UPLOAD_ERR_OK) {
//$id = String::uuid();
if (move_uploaded_file($file['tmp_name'], APP.'webroot/uploads'.DS.$file['name'])) {
$this->request->data['Upload']['upload_id'] = $id;
$this->request->data['Upload']['filename'] = $file['name'];
$this->request->data['Upload']['filesize'] = $file['size'];
$this->request->data['Upload']['filemime'] = $file['type'];
return true;
}
}
return false;
}
function download($filename=null)
{
$data=$this->Upload->find('all',array('conditions'=>array('filename'=>$filename)));
$this->set('val1',$data);
}
}
?>
&#13;
我的download.ctp文件是:
HTML-&GT;图像(&#39;上载/&#39; $ VAL1 [0] [&#39;上传&#39;] [&#39;文件名&#39;]); // echo&#39; Html-&gt; url(数组(&#39;控制器&#39; =&gt;&#39;上传&#39;,&#39;动作&#39; =&gt;&#39;下载&#39;,$ VAL1 [0] [&#39;上传&#39;] [&#39;文件名&#39]。))&#39;&#34;宽度=&#34; 100&#34;高度=&#34; 100&#34; /&GT;&#39 ;; ?&GT;答案 0 :(得分:0)
if (move_uploaded_file($tmp_name['tmp_name'],WWW_ROOT."/img/uploads/".$file['name'])) { /* conditions*/ }
$data=$this->Upload->find('first',array('conditions'=>array('filename'=>$filename)));
echo $this->Html->image('uploads/'.$val1['Upload']['filename'],array('class'=>'media-object img img-thumbnail','alt'=>$val1['Upload']['filename']) );