загрузка файла в cakephp 2.3
Я новичок в CakePHP, и я пытаюсь создать простую загрузку файлов с помощью CakePHP 2,3, вот мой контроллер
public function add() {
if ($this->request->is('post')) {
$this->Post->create();
$filename = WWW_ROOT. DS . 'documents'.DS.$this->data['posts']['doc_file']['name'];
move_uploaded_file($this->data['posts']['doc_file']['tmp_name'],$filename);
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add your post.');
}
}
}
и мой add.ctp
echo $this->Form->create('Post');
echo $this->Form->input('firstname');
echo $this->Form->input('lastname');
echo $this->Form->input('keywords');
echo $this->Form->create('Post', array( 'type' => 'file'));
echo $this->Form->input('doc_file',array( 'type' => 'file'));
echo $this->Form->end('Submit')
он сохраняет имя, фамилию, ключевые слова и имя файла в БД, но файл, который я хочу сохранить в app / webroot / documents, не сохраняется, кто-нибудь может помочь? Спасибо
Обновить
thaJeztah я сделал, как ты сказал, но это дает некоторые ошибки, вот контроллер, если я не ошибаюсь
public function add() {
if ($this->request->is('post')) {
$this->Post->create();
$filename = WWW_ROOT. DS . 'documents'.DS.$this->request->data['Post']['doc_file']['name'];
move_uploaded_file($this->data['posts']['doc_file']['tmp_name'],$filename);
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash('Your post has been saved.');
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash('Unable to add your post.');
}
}
}
и мой add.ctp
echo $this->Form->create('Post', array( 'type' => 'file'));
echo $this->Form->input('firstname'); echo $this->Form->input('lastname');
echo $this->Form->input('keywords');
echo $this->Form->input('doc_file',array( 'type' => 'file'));
echo $this->Form->end('Submit')
и ошибки
Примечание (8): преобразование массива в строку [CORE \ Cake \ Model \ Datasource \ DboSource.php, строка 1005]
Ошибка базы данных: SQLSTATE [42S22]: столбец не найден: 1054 Неизвестный столбец «Массив» в «списке полей»
SQL-запрос: INSERT INTO first.posts (имя, фамилия, ключевые слова, файл_документа) ЗНАЧЕНИЯ ('dfg', 'cbhcfb', 'dfdbd', Array)
и Виктор, я тоже сделал твою версию, она тоже не работает.