Cakephp检索文件数据上传


Cakephp retrieving file data upload

在我的表格上,我有一个上传输入

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

当它试图在"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;
}

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

非法字符串偏移量"名称"

有人可以告诉我如何使用 Cakephp 正确上传!?

您的$_FILE只是一个文件名而不是数组这一事实表明您的表单没有enctype="multipart/form-data"。使用蛋糕,您可以使用FormHelper(http://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