2014-12-07 78 views
-1

我的形式,我有一个上传输入CakePHP的检索文件数据上传

echo $this->Form->input('imagem', array('type'=>'file')); 

,当它试图cheack大小,类型,并调用内部“IMAGEM”输入所有的阵列中的数据,它只是简单的不能。

function uploadImagem() { 
    $file = $this->data['Produto']['imagem']; 
    print_r($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; 
} 

当我打印$文件它应该显示“数组()”,但它检索我的文件的名称。 如果我尝试使用/打印$this->data['Produto']['imagem']['name']$this->data['Produto']['imagem']['error'],它只是给我一个错误

非法串偏移“名”

有人能告诉我怎样才能使用CakePHP正确上传!?

+2

你用过'“型” = >'文件'在'表格 - >创建' – Abhishek 2014-12-07 10:54:33

回答

3

事实上,你的$_FILE只是一个文件名而不是数组表明你的表格没有enctype="multipart/form-data"。 蛋糕,你可以用FormHelperhttp://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-create)实现这一点:

<?= $this->Form->create('Mymodel', array('type' => 'file')) ?> 

而且来管理CakePHP的文件上传,我推荐你使用这个伟大的插件:https://github.com/josegonzalez/cakephp-upload

+0

感谢您的提示!但仍然行不通!你能给我一个链接如何安装或使用这个插件? – 2014-12-07 13:23:43

+0

该文档可在这里找到:http://cakephp-upload.readthedocs.org/en/latest/ – chadrien 2014-12-07 17:29:50