2014-11-22 67 views
-2

我是cakephp和php的新手,我試圖在上傳文件後將文件保存到文件夾中。非法字符串偏移'名稱'[APP Controller PostsController.php,第18行]

public function add() { 
    if ($this->request->is ('post')) { 
     $filename = "app/webroot/img/uploads/" . $this->data ['Post'] ['Image'] ['name']; 
     if ((move_uploaded_file ($this->data ['Post'] ['Image'] ['tmp_name'], $filename))) { 
      $this->Mathang->create(); 
      if ($this->Post->save ($this->request->data)) { 
       $this->Session->setFlash (__ ('save images successful.')); 
       redirect()   
       $this->redirect ('/posts/upload'); 
      } 
     } else { 
      $this->Session->setFlash (__ ('not save images.')); 
     } 
    } 
} 
+0

你可以嘗試'var_dump'-ING $這 - >數據,看看屬性 '名稱' 存在? – EWit 2014-11-22 10:52:40

+0

@Trang Nguyen Quoc,你錯過';'在行尾:'redirect()' – 2014-11-22 11:22:16

+1

你應該在將文件擴展名上傳到web文檔根目錄下的某個地方之前驗證它,否則可以上傳一個'.php'文件。 – Gumbo 2014-11-22 12:06:45

回答

-3
 if ($this->request->is('post')) {   
    $filename=$this->request->data('Post.image'); 

    $filename = "app/webroot/img/uploads/".$_FILES[$filename]['name']; 
    $tempname=$this->request->data('Post.image'); 
    if((move_uploaded_file($_FILES[$tempname']['tmp_name'],$filename)){ 

      if ($this->Post->save($this->request->data)) { 
     $this->Session->setFlash(__('save images successful.')); 
     redirect() 
     $this->redirect('/posts/upload'); 
    }}else{ 
    $this->Session->setFlash(__('not save images.'));} 
} 
+1

如果提供了答案,請花時間來解釋問題是什麼以及如何解決問題,而不是僅轉儲非解析代碼。 – MatsLindh 2014-11-22 12:05:41

+0

它有2個錯誤:未定義的索引:images.jpg [APP \ Controller \ MathangsController.php,第19行]($ filename =「app/webroot/img/uploads /".$_ FILES [$ filename] ['name']] ;),未定義的索引:images.jpg [APP \ Controller \ MathangsController.php,第21行](move_uploaded_file($ _ FILES [$ tempname] ['tmp_name'],$ filename)) – 2014-11-22 16:18:05

+0

如果你在這裏粘貼代碼,至少使其格式有點像CakePHP標準代碼。現在看起來像一團亂七八糟的東西。 – mark 2014-11-22 16:27:46