2

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

さまざまな方法で解決しようとし、問題を検索しましたが、適切な答えが見つかりませんでした。

4

6 に答える 6

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 です。

于 2013-06-20T21:51:53.230 に答える
0

これは、小さなPNGサムネイルに使用する簡単なものです。これらは、「IMAGE」という名前のフィールドの「coreg」というテーブルに格納されます。フィールド タイプは LONGBLOB です。アップロードするたびに、以前の画像が上書きされます。実際のアプリでは、view-file は 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>

画像を表示するコントローラー

よりエレガントなのは、エコーの代わりにビューを使用し、DB ロジックをモデルに移動することですが、これは機能がより優れていることを示しています IMHO:

media.php

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

    }
于 2013-11-10T00:40:37.463 に答える