Я новичок в 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-запрос: ВСТАВИТЬ В first.posts (имя, фамилия, ключевые слова, doc_file) ЗНАЧЕНИЯ ('dfg', 'cbhcfb', 'dfdbd', массив)
и Виктор, я тоже сделал твой вариант, он тоже не работает.