使用codeigniter在mysql数据库中插入图像

时间:2013-06-06 15:47:30

标签: php codeigniter

我正在尝试使用codeigniter将图像存储在mysql中。 数据已成功插入,但我无法查看该图像。

enter code here
$this->gallery_path = realpath(APPPATH . '../img');
    $config=array(
        'allowed_types' => 'jpg|jpeg|gif|png',
        'upload_path' => $this->gallery_path,
        'max_size' =>2000
    );
    $this->load->library('upload',$config);

    $this->upload->do_upload('business_icon');

    $upload_data = $this->upload->data();

        $data_ary = array(
            'title'     => $upload_data['client_name'],
            'file'      => $upload_data['file_name'],
            'width'     => $upload_data['image_width'],
            'height'    => $upload_data['image_height'],
            'type'      => $upload_data['image_type'],
            'size'      => $upload_data['file_size'],
            'date'      => time(),
        );

        $image_data=fopen($this->gallery_path.'/'.$upload_data['file_name'],'rb');
        $image_data1= fread($image_data,$upload_data['file_size']);

        $image_type='image/'.$upload_data['image_type'];



    $insert_data=array(
        'business_name'=>$this->input->post('business_name'),
        'business_icon'=>$image_data1,
        'icon_type'=>$image_type,
    );  

    $this->db->insert('businesses',$insert_data);
    return $this->db->insert_id();

任何人都可以帮我解决这个问题,我如何在db中存储图像,并在codeigniter中读取它。

1 个答案:

答案 0 :(得分:1)

a)确保字段business_icon的类型在mysql中是longblob。

b)确保在阅读时输出正确的标题(Content-Type)和数据,不要使用以下内容:

<img src="<?php echo $data;?>">