私は WAMP サーバーを使用しており、CI を使用してデータベースに画像をアップロードしたいと考えています。データベースの画像変数は blob データ型です。私の質問は次のとおりです。
1) ファイル名の代わりに画像を保存する方法と、どのデータ型を使用すればよいですか?
2) 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; ?>
さまざまな方法で解決しようとし、問題を検索しましたが、適切な答えが見つかりませんでした。