CakePHP文件上传错误

时间:2014-01-08 07:51:10

标签: php cakephp file-upload cakephp-2.0

我在数据库中遇到这个错误,说我的数组存在问题:

  

数据库错误

     

错误:SQLSTATE [42S22]:未找到列:1054'字段列表'中的未知列'数组'

     

SQL查询:更新`mongexc_cake214` .configurations` SET`id` = 1,`username` ='bill clinton',`profession` ='president',`description` ='Ob Jones-D是泰语按摩和电子针灸专家。 Lorem ipsum dolor坐在amet,consectetur adipisicing elit。 Quam,repellat optio officiis neque ea repudiandae sint corrupti illo? Maiores adipisci mollitia quae perferendis numquam minima deserunt ratione placeat rem。 Numquam?',`tel_mobile` ='000-000-0000',`地址'='000非洲纽约V99 999',`userphoto` =数组WHERE`mongexc_cake214` .configurations``id` ='1 “

     

注意:如果要自定义此错误消息,请创建app / View / Errors / pdo_error.ctp

这是 edit.php 文件的一部分,其中 userphoto 字段用于上传图片文件

<?php echo $this->Form->create('Configuration', array('type' => 'file')); ?>
<fieldset>
<legend><?php echo __('Edit Configuration'); ?></legend>
<?php
echo $this->Form->input('id');
echo $this->Form->input('username');
echo $this->Form->input('profession');
echo $this->Form->input('description', array('type' => 'textarea','label' => 'Content of this Article', 'rows' => '10', 'cols' => '120'));
echo $this->Form->input('userphoto', array('type' => 'file'));
echo $this->Form->input('tel_mobile');
echo $this->Form->input('address');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>

2 个答案:

答案 0 :(得分:0)

userphoto是一个文件字段,它将包含类似以下的数据:

$this->request->data['Configuration']['userphoto'] = array(
    'error' => …,
    'name' => …,
    …
)

这意味着它是一个数组。

但是在您的代码中,您直接将数组插入数据库。您需要明确处理文件上传。

查看Best practice to upload files in CakePHP

答案 1 :(得分:0)

$imageData = exif_imagetype($this->request->data['Foo']['image']['tmp_name']);  
                image_type_to_mime_type($imageData);  
                    switch ($imageData) :  
                    case '2':  
                        $type = '.jpg';  
                        break;  
                    case '3':  
                        $type = '.png';   
                        break;  
                    default:  
                        $type = 'invalid';  
                        break;  
                    endswitch;  
$uniq = mt_rand();  
move_uploaded_file($this->request->data['Foo']['image']['tmp_name'],'path/to/dir');  
$this->request->data['Foo']['image'] = '/path/to/dir/'.$uniq.$type;