繁体   English   中英

非法字符串偏移“错误”

[英]Illegal string offset 'error'

我是Cakephp和php的新手,我尝试在上传文件后将文件保存在文件夹中。 我正在遵循本教程http://www.tuxradar.com/content/cakephp-tutorial-build-file-sharing-application

我有以下代码:

    function uploadFile() {    
        $file = $this->data['Upload']['file'];  
  if ($file["error"] === UPLOAD_ERR_OK) {  
    $id = String::uuid();  
    if (move_uploaded_file($file['tmp_name'], APP.'uploads'.DS.$id)) {  
      $this->data['Upload']['id'] = $id;  
      $this->data['Upload']['user_id'] = $this->Auth->user('id');  
      $this->data['Upload']['filename'] = $file['name'];  
      $this->data['Upload']['filesize'] = $file['size'];  
      $this->data['Upload']['filemime'] = $file['type'];  
      return true;  
    }  
  }  
  return false;  
}  

它给了我这个错误: Illegal string offset 'error'
我尝试了这个"var_dump($file["error"]);"
它正在返回string(1) "1"
我的问题是什么,我该如何解决。
谢谢

我认为问题在于,由于下载没有引起任何错误,这就是为什么错误索引不存在的原因,这就是为什么我建议您在测试中编写的原因:

if(isset($file['error']) && $file['error'] === UPLOAD_ERR_OK){
  //your code here
}

我希望我会有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM