【问题标题】:Storing and retrieving image in sql server using Codeigniter使用 Codeigniter 在 sql server 中存储和检索图像
【发布时间】:2013-12-03 08:30:39
【问题描述】:

我想将图像存储在 使用 Codeigniter 的 SQL 服务器;实际上我可以选择图像并转换它,但是当我这样做并将其存储在数据库中然后检索它时,图像无法显示。

我在数据库中观察到,以前使用 C# 存储的图像的名称以 0xFFD8F 开头,但使用 codeigniter 时,名称以 0x3 或其他任何名称开头。

这里是控制器

public function addImage() {
    $config['upload_path'] = './uploads/';
    $config['allowed_types'] = 'gif|jpg|png';

    $this->load->library('upload', $config);

    if ( ! $this->upload->do_upload()) {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('Profile/addImage', $error);
    } else {
        $data = $this->upload->data(); 
        $dataString = file_get_contents($data['full_path']);
        $orig_name = $data['orig_name'];
        $uploadImage = $this->Personalinfo_model->uploadImage(array('orig_name' => $orig_name, 'dataString' => $dataString ));
        delete_files($data['full_path']) ;
    }
}

型号

function uploadImage($options = array()) {
    $orig_name = $options['orig_name']; 
    $dataString = $options['dataString']; 
    // $hex_image = bin2hex($dataString);
    $data = unpack("H*hex", $dataString);
    $object = array(
        'EmployeeID' => '3',
        'filename' => $orig_name,
        'EmployeePic' => "0x".$data['hex']
    );
    $this->db->where('EmployeeID','3');
    $this->db->update('EmployeePic', $object);
}

解压图片后会发生错误,但我无法检测到问题。

【问题讨论】:

  • 您应该只保存图像路径而不是图像本身。否则,您最终会在数据库服务器中获得大量存储和糟糕的性能。
  • 我使用 file_get_contents($data['full_path']); 获得完整路径;
  • 文件获取路径将返回文件信息,而不是文件本身。您会做的是存储图像名称或带有名称的图像路径然后回显它。
  • 但是 $data['full_path'] 在没有任何信息的情况下自行返回文件,如果不是,那我应该使用什么。
  • $data['full_path'] 的输出是什么?

标签: php sql-server codeigniter


【解决方案1】:

正如我们在聊天方面所讨论的,您的功能如下:

function uploadImage($options = array()) { 
    $orig_name = $options['orig_name']; 
    $dataString = $options['dataString']; 
    $hex_image = bin2hex($dataString); 

    $object = array( 
        'EmployeeID' => '3', 
        'filename' => $orig_name, 
        'EmployeePic' => "0x".$hex_image 
    ); 
    $this->db->where('EmployeeID','3'); 

    $this->db->update('[HumanResource].[dbo].[EmployeePic]', $object); 
}

您正在将0x 字符串连接到您的二进制文件。通过删除它,您应该可以使您的功能正常工作。

【讨论】:

    猜你喜欢
    • 2011-01-05
    • 2014-08-13
    • 2011-01-26
    • 2010-12-12
    • 2013-06-17
    • 2014-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多