我是CakePHP的新手,尝试上传文件,move_uploaded_file
始终返回false
。这是我的代码:
编辑:第一行是错误的
// This line was wrong, should be done after uploading.
$this->request->data['Post']['image'] = $this->request->data['Post']['image']['name']
if($this->Post->save($this->request->data)) {
$filename = basename($this->request->data['Post']['image']['name']);
$filePath = WWW_ROOT . DS . 'files' . DS . $filename;
if(move_uploaded_file(
$this->request->data['Post']['image']['tmp_name'],
$filePath
))
$this->Session->setFlash(__('Your post has been saved'));
else
$this->Session->setFlash(__('Your post has been saved, but image failed'));
}
为什么不起作用?
答案 0 :(得分:0)
解决。我在上传之前设置了图片名称,好像应该在上传文件后完成。
//move_uploaded_file() stuff...
$this->request->data['Post']['image'] = $filename;
if($this->Post->save($this->request->data)) { //...