【发布时间】: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