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

CodeIgniter: How can I store a photo name into database

我找了很多关于它的例子,我做了一些我能做的。

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

我做了什么

我需要你帮助我

下面的代码。

型号

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。它将写在控制器或模型上?我基本上该怎么做?

如果您想将内容插入数据库,您需要为 $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);