2014-11-22 44 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