CakePHP move_uploaded_file返回false

时间:2014-01-10 22:21:15

标签: php cakephp

我是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'));
}

为什么不起作用?

1 个答案:

答案 0 :(得分:0)

解决。我在上传之前设置了图片名称,好像应该在上传文件后完成。

//move_uploaded_file() stuff...

$this->request->data['Post']['image'] = $filename;

if($this->Post->save($this->request->data)) { //...