海我是 cakephp 的新手。我创建了一个包含两个字段名称和图像文件的示例表单。我能够将图像名称和 uername 保存在数据库表中,并将图像保存在 webroot 文件夹中。现在我想使用特定的检索图像id.如何使用特定的用户 id 从 webroot 文件夹中检索图像?请帮助我提高我对 cakephp 的了解。
添加.ctp
<?php
echo $this->Form->create('User', array('controller' => 'users', 'action' =>
'add','type' => 'file','enctype' => 'multipart/form-data'));
echo $this->Form->input('name');
echo $this->Form->input('image', array('type' => 'file'));
echo $this->Form->end('Submit');
?>
用户控制器.php
<?php
class UsersController extends AppController {
public $helpers = array('Html', 'Form');
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add','retrieve');
}
public function retrieve()
{
$this->loadModel('User');
$ret = $this->User->find('all');
$this->set('retrieve', $ret);
}
public function add()
{
if ($this->request->is('post')) {
$this->User->create();
$this->loadModel('User');
$id = $this->request->data['User']['id'];
$pathname = $this->request->data['User']['image']['tmp_name'];
$filename = $this->request->data['User']['image']['name'];
$this->request->data['User']['image'] = $filename;
if($this->User->save($this->request->data))
{
move_uploaded_file($pathname, $_SERVER['DOCUMENT_ROOT'] . '/cakephp_image
/app/webroot/files/' . $filename);
$this->Session->setFlash(__('The Image has been saved'));
return $this->redirect(array('controller' => 'users', 'action' =>
'retrieve'));
}
else
{
$this->Session->setFlash(__('The Image could not be saved. Please, try again.'));
}
}
}
}
?>