CodeIgniter:如何将照片名称存储到数据库中

时间:2015-02-27 08:44:14

标签: codeigniter upload photo

我曾经找过很多关于它的例子,我做了一些我能做的事情。

我将建立一个注册表。将有五个文本字段和一个照片上传部分。

我做了什么

  • 文本字段存储在数据库中。在这里只显示一个字段。 (成功地)
  • 在提交表格(成功)后,照片会存储在文件夹中

我需要你帮助我

  • 照片的名称未存储在数据库中。它存储在文件夹中。

以下代码。

模型

public function registration($post)
{
   $this->db->insert('registration', $post); 
} 

控制器

function do_upload()
{
    $post = $this->input->post();
    $this->form_validation->set_rules('Name','Name','trim|required|xss_clean');

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

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());
        $this->load->view('example/registration_form', $error);
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
        $result = $this->register_model->registration($post);
        $this->load->view('upload_success', $data, $result);
    }
}

查看

<?php echo form_open_multipart('upload/do_upload');?>
    <input type="file" name="userfile" size="20" />
    <br />
    <input type="text" name="name" class="form-control" placeholder="Your name">

    <input type="submit" value="upload" />
</form>

数据库上是photo_name。它将写在控制器或型号上?我基本上该怎么办?

1 个答案:

答案 0 :(得分:3)

如果要将内容插入数据库,则需要为$this->do_upload->data()使用变量。

示例:$data_file = $this->do_upload->data();

示例:$data_file['file_name']

$data = array(
'file_name' => $data_file['file_name'],
'file_type' => $data_file['file_type'],
'full_path' => $data_file['full_path'],
'raw_name' => $data_file['raw_name'],
'orig_name' => $data_file['orig_name'],
'client_name' => $data_file['client_name'],
'file_ext' => $data_file['file_ext'],
'file_size' => $data_file['file_size'],
'is_image' => $data_file['is_image'],
'image_width' => $data_file['image_width'],
'image_height' => $data_file['image_height'],
'image_type' => $data_file['image_type'],
'image_size_str' => $data_file['image_size_str']
);

$this->db->where('whatever', $whatever);
$this->db->update('tablename', $data);

或者

$data = array(
'file_name' => $data_file['file_name'],
'file_type' => $data_file['file_type'],
'full_path' => $data_file['full_path'],
'raw_name' => $data_file['raw_name'],
'orig_name' => $data_file['orig_name'],
'client_name' => $data_file['client_name'],
'file_ext' => $data_file['file_ext'],
'file_size' => $data_file['file_size'],
'is_image' => $data_file['is_image'],
'image_width' => $data_file['image_width'],
'image_height' => $data_file['image_height'],
'image_type' => $data_file['image_type'],
'image_size_str' => $data_file['image_size_str']
);


$this->db->insert('tablename', $data);