如何在cakephp中在数据库中插入图像名称?
到目前为止,我已经实现了以下功能 我面临的问题是我无法更新文件名,因此我得到一个错误,导致文件无法上传 控制器:如何在cakephp中在数据库中插入图像名称?,cakephp,insert,save,Cakephp,Insert,Save,到目前为止,我已经实现了以下功能 我面临的问题是我无法更新文件名,因此我得到一个错误,导致文件无法上传 控制器: public function add(){ if (!empty($this->data)) { if (!empty($this->data['Note'])){ $filename = basename($this->data['Note']['note_image']['name']);
public function add(){
if (!empty($this->data)) {
if (!empty($this->data['Note'])){
$filename = basename($this->data['Note']['note_image']['name']);
move_uploaded_file($this->data['Note']['note_image']['tmp_name'],WWW_ROOT . 'files' . DS . $filename);
if($this->Note->save($this->data['Note'])){
$this->Session->setFlash('The note has been saved');
$this->redirect(array('action' => 'add'));
}
}
}
}
视图:
我认为保存功能将不存在。有没有办法做到这一点。控制器的代码应该是这样的:
视图应如下所示:
我得到一个错误-错误应该在问题中。另外,请用您正在使用的CakePHP版本(看起来像2.xDear)标记您的问题,我认为您的帖子会很有用,但我收到一个错误:“致命错误:对成员函数的调用在第34行的E:\wamp\www\cake\app\controllers\notes\u controller.php中的非对象上”。你还有别的解决办法吗。但我还是要感谢您的尝试。尝试一下:如果$this->request->is'post',这是针对CakePHP2.x版本的
<?php
echo $this->Form->create('Note', array('type' => 'file'));
echo $this->Form->input('note_title');
echo $this->Form->input('note_desc');
echo $this->Form->input('note_image', array('type' => 'file'));
echo $this->Form->end('Add');
?>
public function add(){
if ($this->request->is('post') || $this->request->is('put')) {
$file = $this->request->data['Note']['note_image'];
if($file['error'] === UPLOAD_ERR_OK)
{
$filename = $file['name'];
if(move_uploaded_file($file['tmp_name'],WWW_ROOT . 'files' . DS . $filename))
{
$this->request->data['Note']['note_image'] = $filename;
if($this->Note->save($this->request->data)){
$this->Session->setFlash('The note has been saved');
$this->redirect(array('action' => 'add'));
}
}
}
else
{
$this->Session->setFlash('ERROR');
$this->redirect(array('action' => 'add'));
}
}
}
<?php
echo $this->Session->flash();
echo $this->Form->create('Note', array('enctype'=>'multipart/form-data'));
echo $this->Form->input('note_title');
echo $this->Form->input('note_desc');
echo $this->Form->input('note_image', array('type' => 'file'));
echo $this->Form->end('Add');
?>