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

非法字符串偏移量';名称';[APP\Controller\PostsController.php,第18行],php,cakephp,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->d

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

你能试试
var_dump
-ing$this->data看看属性'name'是否存在吗?@Trang Nguyen qooc,你错过了
在行尾:
redirect()
在将文件扩展名上载到web文档根目录下的某个位置之前,应验证文件扩展名,否则可以上载
.php
文件。如果提供答案,请花时间解释问题所在以及如何解决,它有两个错误:未定义的索引:images.jpg[APP\Controller\MathangsController.php,第19行]($filename=“APP/webroot/img/uploads/”$\u FILES[$filename]['name'];),未定义的索引:images.jpg[APP\Controller\MathangsController.php,第21行](移动上传的文件($\u FILES[$tempname]['tmp\u name'],$filename))如果你在这里粘贴代码,请至少让它的格式有点像CakePHP标准代码。现在看起来像是一团乱麻。
     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.'));}
}