0

我想知道如何在使用 codeigniter 时上传图像并将其名称保存在数据库中,我为此使用了简单的表单标签,例如,

   <form  action="" name="add" enctype="multipart/form-data" method="post">

   <label for="img">Image: </label> <input type="file" name="image" />

   <label for="title">Title: </label> <input type="text" name="photo_title" value="" />

   <label for="description">Description: </label>
   <textarea name="photo_description">   </textarea>

   <label for="photo_category">Category </label>
     <select name="photo_category">
          <option value="select_one">Please Select..</option>
           <option value="fun">Fun</option>
      </select>
     <input type="submit" value="Add" />

   </form> 

我检查了手册,但无法理解他们在做什么,请任何人帮助我为此创建一个控制器功能,该功能将在数据库中插入所有值并上传图像文件。

谢谢,沙鲁克

4

2 回答 2

0

通过使用

  $this->upload->initialize($config);

代替

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

谢谢,

沙鲁克

于 2013-01-09T09:55:13.520 回答
0
public function upload() {

    $this->load->library('form_validation');

    $this->form_validation->set_rules('photo_title', 'Photo Title', 'required');
    $this->form_validation->set_rules('photo_description', 'Photo Description', 'required');
    $this->form_validation->set_rules('photo_category', 'Photo Category', 'required');

    if ($this->form_validation->run() == TRUE) {

        $data = array(
               'photo_title' => $this->form_validation->set_value('photo_title'),
               'photo_description' => $this->form_validation->set_value('photo_description'),
               'photo_category' => $this->form_validation->set_value('photo_category'),,
        );
        $this->db->insert('table_name', $data);


        // Start Upload Picture
        if (!empty($_FILES['image']['name'])) {
            $config['upload_path'] = ''.$_SERVER['DOCUMENT_ROOT'].'/upload_path/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '5000';
            $config['max_width']  = '2000';
            $config['max_height']  = '2000';
            $config['encrypt_name'] = TRUE;
            $config['remove_spaces'] = TRUE;

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

            if ($this->upload->do_upload('image')) {

                $data = array('upload_data' => $this->upload->data());
                $data = $this->upload->data();

                $this->db->select('id');
                $this->db->from('table_name');
                $this->db->order_by('id', 'DESC');
                $this->db->limit(1);
                $query = $this->db->get();
                $row = $query->row();

                $data1 = array(
                   'shirt' => $data['file_name'],
                );
                $this->db->where('id', $row->id);
                $this->db->update('table_name', $data1);

            }

        }

    };

}
于 2013-01-08T15:31:21.860 回答