Php 使用Codeigniter在sql server中存储和检索图像
我想使用Codeigniter将图像存储在SQL server中;实际上,我可以选择图像并将其转换,但当我这样做并将其存储在数据库中,然后检索时,图像无法显示 我在数据库中观察到,使用C#存储的图像的前一个名称以0xFFD8F开头,但使用codeigniter时,名称以0x3或其他任何名称开头 这里是控制器Php 使用Codeigniter在sql server中存储和检索图像,php,sql-server,codeigniter,Php,Sql Server,Codeigniter,我想使用Codeigniter将图像存储在SQL server中;实际上,我可以选择图像并将其转换,但当我这样做并将其存储在数据库中,然后检索时,图像无法显示 我在数据库中观察到,使用C#存储的图像的前一个名称以0xFFD8F开头,但使用codeigniter时,名称以0x3或其他任何名称开头 这里是控制器 public function addImage() { $config['upload_path'] = './uploads/'; $config['allowed_ty
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);
}
解压图像后,可能会发生错误,但我无法检测到问题。正如我们在聊天侧讨论的,您具有以下功能:
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
字符串连接到二进制文件。通过移除它,您的功能应该能够正常工作 您应该只保存图像路径,而不是图像本身。否则,数据库服务器中的存储量将非常大,性能也会很差;文件获取路径将返回文件信息,而不是文件本身。您要做的是存储图像名称或具有名称的图像路径,然后只回显它。但是$data['full_path']返回文件本身而不包含任何信息,如果没有,那么我应该使用什么。$data['full_path']
的输出是什么?