小编bri*_*p09的帖子

如何使用CodeIgniter在数据库中存储和检索图像

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

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

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

我的控制器代码:

<?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;
}   
}
?>
Run Code Online (Sandbox Code Playgroud)

我的模特的代码:

<?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 …
Run Code Online (Sandbox Code Playgroud)

php mysql html5 codeigniter

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

codeigniter ×1

html5 ×1

mysql ×1

php ×1