如何使用CodeIgniter在数据库中存储和检索图像 [英] how to store and retrieve images in database using CodeIgniter

查看:47
本文介绍了如何使用CodeIgniter在数据库中存储和检索图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WAMP服务器,我想使用CI上载数据库中的图像.数据库中的图像变量是blob数据类型.我的问题如下:

I am using a WAMP server, and I want to upload images in the database using CI.The image variable in database is of blob datatype. My question's as follows:

1)如何存储图像而不是文件名以及应该使用哪些数据类型?

1) How to store the image instead of the file name and what datatypes should I use?

2)如何从数据库中检索图像?

2) How to retrieve images from the DB?

我的控制器的代码:

<?php class Image_control extends CI_Controller{
function index()
{
    //$this->load->view('image_view');

    //$this->Image_model->do_upload();

    $data['images']=$this->Image_model->get_images();
    $this->load->view('image_view',$data);  
}
function do_upload()
{
    $config = array(
        'allowed_types' => 'jpg|png|bmp', 
        'upload_path'=>'./images1/',
        'max_size'=>2000
    );
    $this->load->library('upload',$config);
    if (!$this->upload->do_upload()) {
        $errors[]=array('error'=>$this->upload->display_errors());
        $this->load->view('image_view',$errors);
    }
    $image_path=$this->upload->data();
    $file_name=$image_path['file_name'];
    $config = array(
        'a_name' => $this->input->post('a_name'),
        'a_details'=>$this->input->post('a_info'),
        'a_photo'=>$file_name
    );
    $insert=$this->db->insert('animalstore',$config);
    return $insert;
}   
}
?>

我的模型的代码:

<?php class Image_model extends CI_Model {
function get_images()
{
    $query = $this->db->get('animalstore');
    if($query->num_rows > 0 )
    {
        foreach($query->result() as $rows)
        {
            $data[] = $rows;
        }
        return $data;
    }
}
}
?>

最后是我的视图代码:

<?php
    echo form_open_multipart('image_control/do_upload');
    echo form_input('a_name','Animal Name');
    echo form_input('a_info','Animal Information');
    echo form_upload('userfile');
    echo form_submit('upload','Upload');
    echo form_close();
?>

<?php foreach ($images as $image):?>
<h1><?php echo $image->a_name;?></h1>
<h1><?php echo $image->a_details;?></h1>
<img src = "/<?php// echo ltrim($image->a_photo, '/'); ?>" >
<img src="http://localhost/ci_test/images1/<?php echo $image->a_photo;?>"/> 
<img src="<?php //echo sprintf("images/%s", $image['screenshot']);?>" />
<h1><?php// echo $image->a_photo;?></h1>
<?php endforeach; ?>

我尝试用不同的方式解决它并搜索了我的问题,但是找不到合适的答案.

I tried solving it in different ways and searched for my problem but I didn't find any appropriate answer.

推荐答案

请勿在数据库内存储文件!

DO NOT STORE FILES INSIDE THE DATABASE!!!

这始终是一个糟糕的设计思想.将文件存储在文件系统中,只需存储文件名并指向文件,这将在以后为您省去很多麻烦.

This is always a bad design idea. Store the files in the file system and simply store the file names and point to the file, it will save you a lot of headaches in the future.

这篇关于如何使用CodeIgniter在数据库中存储和检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆