2012-03-06 138 views
0

嗨我試圖創建一個文件上傳系統,我可以上傳一個文件,它會將文件名更改爲上傳到表的MYSQL ID。 這是我的代碼...CakePHP 2.0文件上傳不起作用

function add() { 
    if (!empty($this->data)) { 
     $this->Upload->create(); 
     if ($this->uploadFile() && $this->Upload->save($this->data)) { 
      $this->Session->setFlash(__('<p class="uploadflash">The upload has been saved</p>', true)); 
      $this->redirect(array('action' => 'add')); 
     } else { 
      $this->Session->setFlash(__('<p class="uploadflash">The upload could not be saved. Please, try again.</p>', true)); 
     } 
    } 
     } 

function uploadFile() { 
    $file = $this->request->data['Upload']['file']; 
    if ($file['error'] === UPLOAD_ERR_OK) { 
     if (move_uploaded_file($file['tmp_name'], APP.'webroot/files/uploads'.DS.$this->Upload->id.'.mp4')) { 
      $this->Upload->save($this->data); 
      return true; 
     } 
    } 
    return false; 
} 

但是文件沒有被上傳到了目錄,但該信息被上傳到SQL田部。

我不明白爲什麼這個函數$ this-> Upload-> id不適用於文件重命名? 如果我把它放在語音標記中,那麼它將文件重命名爲「$ this-> Upload-> id.mp4」,但是我希望它更像114.mp4,如果那是保存信息的字段。 有人有什麼想法嗎?

在此先感謝

回答

0

您需要先保存。 $ this-> Upload-> id在你的情況下是空的。您的興趣: $ this-> Upload-> create();清除$這個 - > Upload-> ID

參見:

http://api20.cakephp.org/class/model#method-Modelcreate http://api20.cakephp.org/view_source/model#line-1381

+0

非常感謝你這工作,它只是一種先保存然後重命名的情況。非常感激! – 001221 2012-03-06 13:53:43