2013-06-20 33 views
2

我正在使用WAMP服務器,並且想要使用CI在數據庫中上傳圖像。數據庫中的圖像變量是blob數據類型。我的問題如下:如何使用CodeIgniter在數據庫中存儲和檢索圖像

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; 
} 
} 
?> 

我的模型代碼:

<?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; ?> 

我試着以不同的方式解決問題並搜索我的問題,但沒有找到任何合適的答案。

+3

不要將圖像存儲在數據庫中http://stackoverflow.com/questions/6472233/can-i-store-images-in-mysql – user20232359723568423357842364

回答

6

請勿將文件存儲在數據庫中!

這總是一個糟糕的設計理念。將文件存儲在文件系統中,並簡單地存儲文件名並指向文件,這將爲您在將來節省很多麻煩。

+1

-1爲過時的想法。 – gustavohenke

+1

我不同意,當服務器在羣集上時會發生什麼?以及備份上發生了什麼?因爲我最好的設計方法是將圖像存儲在數據庫中。 –

+1

-1用於提供意見而不是添加解決方案。可移植性例如是在數據庫中存儲圖像(或其他文件)的許多優點之一。 –

2
// uploading 
public function do_upload(){ 
... 

$image_path=$this->upload->data(); 
$uploaded_image = $image_path['full_path']; 

// Read the file 
$fp = fopen($uploaded_image, 'r'); 
$data = fread($fp, filesize($uploaded_image)); 
$data = addslashes($data); 
fclose($fp); 

// here you can easy insert $data to 'a_photo' column.  

} 


// Viewing, $image_id is row id 
public function getImage($image_id){ 

// select $row from database as usual and then 

$content = $row['a_photo']; 
echo '<img src="data:image/jpeg;base64,'.base64_encode($content).'">'; 
} 

在模板:

<?php getImage(12); ?> 

,其中12是行ID。

+1

爲什麼要在模板/視圖中調用控制器方法? –

+0

這只是一個展示它如何工作的例子。在現實世界中,getImage()將在控制器中調用,結果賦給某個模板變量,然後顯示在模板中。而在現實世界中,TravisO的回答是完全正確的:) – ToxaBes

0

試試這個代碼

機型代碼

function do_upload() { 

    $config = array(
      'allowed_types' => 'jpg|png|bmp', 
      'upload_path'=>'./images1/', //make sure you have this folder 
      'max_size'=>2000 
     ); 

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

     if ($this->upload->do_upload()) { 
      echo "Upload success!"; 
     } else { 
      echo "Upload failed!"; 
     } 
    $image_data = $this->upload->data(); 

    } 

function get_images() 
    { 
     $query = $this->db->get('animalstore'); 
     return $query; 
    } 

function Save_gallery($in) 
{ 
$save=$this->db->insert('animalstore',$in); 
return $save; 
} 

控制器代碼

function index() 
{ 
    $this->load->model('Image_control'); //call a models 

    if ($this->input->post('upload')) { 

    $in=array(); 

    $in['a_name'] = $this->input->post('a_name'), 
    $in['a_details'] = $this->input->post('a_info'), 
    $in['a_photo']=$_FILES['userfile']['name']; 

    if($this->Image_model->do_upload()) { 

    echo $this->upload->display_errors(); 

    }else { 

    $this->Image_model->Save_gallery($in); 

    header('location:index'); 
    } 

    $data['images']=$this->Image_model->get_images(); 
    $this->load->view('image_view',$data); 
} 

視圖

<?php 
    echo form_open_multipart('image_control/index'); 
    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> 
<?php echo '<img src ="'. base_url().'images1/'.$image['a_photo'].'" >"; 
endforeach; ?> 
+0

這不會將它存儲在數據庫中 –

0

這裏有一個快速的東西我用小PNG縮略圖。 這些存儲在名爲「IMAGE」的字段中的名爲「coreg」的表中。字段類型是LONGBLOB。每次上傳都會覆蓋以前的圖像。在實際應用中的視圖文件顯示爲一個iframe:

瀏覽文件上傳(最好使用過程中的CI特定形式的標籤,但你的想法)

add_image。PHP:

Current image: 
    <img src='/media/png/coreg/<?=$coregID?>' /> 
    <? if(isset($error)){ echo $error; }?> 
    <form method='post' enctype="multipart/form-data" action='add_image/<?=$coregID?>'> 
     <input name="userfile" type="file" class='vLink' /> 
     <input name="submitbtn" type="submit" value=" upload &amp; overwrite " class='eLink' /> 
    </form> 

CONTROLLER示出圖像

更優雅將是使用一個視圖而不是回聲和到DB邏輯移入模型,但是這示出了功能性更好IMHO:

忽略原始

require_once dirname(__FILE__) . "/base.php"; 

     class Media extends BaseController { 

     function __construct() { 
      parent::__construct(); 
     } 


     function png($table,$id) { 
      $this->db->where('ID',$id); 
      $r = $this->db->get($table); 
      if($r->num_rows){ 
       $r = $r->result_array(); 
       header("Content-Type: image/png"); 
       echo $r[0]['IMAGE']; 
      } 
     } 


    } 

控制器上傳圖片:

function add_image($coregID){ 
     $data['coregID'] = $coregID; 
     $data['error'] = ''; 
     if(isset($_POST['submitbtn'])){ 
      $config['upload_path'] = './assets/img/coreg/'; 
      $config['allowed_types'] = 'png'; 
      $config['max_size'] = '100'; 
      $config['max_width'] = '350'; 
      $config['max_height'] = '350'; 
      $config['file_name'] = $coregID.".png"; 
      if(file_exists($config['upload_path'].$config['file_name'])){ 
       unlink($config['upload_path'].$config['file_name']); 
      } 
      $this->load->library('upload', $config); 

      if (! $this->upload->do_upload()){    
       $data['error'] = $this->upload->display_errors(); 
      } else { 
       $this->upload->data(); 
       // now move the image into the DB 
       $fp = fopen($config['upload_path'].$config['file_name'], 'r'); 
       $data = fread($fp, filesize($config['upload_path'].$config['file_name'])); 

       $this->db->where('ID',$coregID); 
       $this->db->update('COREG',array('IMAGE' =>$data)); 
       fclose($fp); 
       // optionally delete the file from the HD after this step 
       //unlink($config['upload_path'].$config['file_name']); 
      } 
     } 

      $this->load->view("add_image", $data); 

    } 
相關問題