非法字符串偏移'名称' [APP \ Controller \ PostsController.php,第18行]

时间:2014-11-22 10:33:10

标签: php cakephp

我是cakephp和php的新手,我试图在上传后将文件保存到文件夹中。

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

1 个答案:

答案 0 :(得分:-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.'));}
}