1

画像データをデータベースに保存する方法を学びました。それらはファイル名とパスです。パスはデータベースに表示されますが、ファイル名には表示されません。どうしたの?

これがコントローラー、

function do_upload() {
    $config['upload_path']='./uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '100';
    $config['max_width'] = '1024';
    $config['max_height'] = '768';

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

    if(!$this->upload->do_upload()){
        $error = array('error' => $this->upload->display_errors());

        $this->load->view('upload_form', $error);
    } else {
        $data  = array('upload_data' => $this->upload->data());

        $this->load->view('upload_success', $data);
    }
}

そしてモデル

    function upload ($config) {
    $config = $this->upload->data();
    $upload_data = array(
        'path' => $config['full_path'],
        'nama_foto' => $config['file_name']
    );

    $this->db->insert('tb_picture', $upload_data);
}

そしてテーブル ここに画像の説明を入力

私は何をすべきか?

前にありがとう。

4

1 に答える 1

1

これを読む前に、自分でもう一度試すか、Web http://net.tutsplus.com/sessions/codeigniter-from-scratch/にあるあらゆる種類の viedotutorial を試してください。

コントローラー関数は次のようになります

function do_upload()
    {
        $config['upload_path']='./uploads/'; //needs to set uploads folder CHMOD 0644
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '100';
        $config['max_width'] = '1024';
        $config['max_height'] = '768';

        $config['overwrite']  = FALSE;
        $config['remove_spaces']  = TRUE;

        $field_name = "userfile"; //name tag in our HTML form in case you want to change it

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

        if ( ! $this->upload->do_upload($field_name)) //upload happens
        {
            $error = array('error' => $this->upload->display_errors());
            $this->load->view('upload_form', $error);
        }    
        else
        {
             //succesful upload get data from upload and use them with our model
             $upload_data = $this->upload->data();
             $this->upload_model->upload($upload_data);
        }
    }    

モデル関数

function upload ($data) {

    if (empty($data) || $data === FALSE) return FALSE;

    $insert_data = array(
        'path' => $data['full_path'],
        'nama_foto' => $data['file_name']
    );

   $this->db->insert('tb_picture', $insert_data);

   return $this->db->insert_id(); //returns last inserted ID
}

アップロード機能を使用したモデルは完全に「間違っている」ことに注意してくださいdata。モデルが処理できるように、モデルにのみ渡すようにしてください。

于 2013-09-16T15:35:58.837 に答える