非法字符串偏移'错误'

时间:2014-11-04 21:40:15

标签: php cakephp file-upload

我是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"
我的问题是什么?如何解决? 感谢

1 个答案:

答案 0 :(得分:-1)

我认为问题是因为下载没有导致任何错误,这就是错误索引不存在的原因,这就是为什么我建议你写下你的测试:

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

我希望我能提供帮助。

相关问题