2014-11-04 98 views
0

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

+0

查看$ this-> data ['Upload']或检查数组键'upload'。 – Knase 2014-11-04 21:50:45

+0

我刚刚试图删除['上传'] ['文件']和var_dumb给我阵列(2){[“上传”] =>数组(3){[“标题”] =>字符串(2)“ mm“[”description“] => string(0)”“[”file“] => string(24)”15您不知道Me.mp3“} [”User“] => array(1){ [“User”] => array(1){[0] => string(1)“3”}}}。现在我有一个问题:未定义的索引:错误 – user3768804 2014-11-04 22:07:55

+0

你用过吗?<?php echo $ this-> Form-> create('Upload',array('type'=>'file'));?>'? – Abhishek 2014-11-05 06:17:57

回答

-1

我认为这个问题是因为下载并未造成任何错误,这是为什么错误索引不存在,这就是为什么我建议你在测试写:

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

我希望我有帮助。

+0

不幸的是我试过了,我仍然得到相同的错误。它给出了错误:未定义的索引:错误,名称,大小,类型,tmp_name。我猜测这是因为它们不是数组的一部分。我想我需要尝试一个不同的功能。 – user3768804 2014-11-05 05:05:21

+0

这个答案中的条件永远不会通过。有些东西不能同时设置(条件的第一部分)_and_等于某些东西(第二部分)。从条件的第一部分删除否定。 – 2014-11-05 13:02:23