2014-01-10 50 views
0

我是CakePHP的新手,嘗試上傳文件,move_uploaded_file總是返回false。這裏是我的代碼: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

看到這個http://stackoverflow.com/a/20989968/1239506它可能會幫助你 –

回答

0

已解決。我在上傳之前設置了圖像名稱,似乎應該在上傳文件後完成。

//move_uploaded_file() stuff... 

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

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