如何将图像路径和名称上传到数据库 - Codeigniter

时间:2013-04-24 11:07:30

标签: php mysql codeigniter upload insert

我知道这个问题已被多次询问,但我发现的所有其他问题都与我想要的不同。

我想将文件名和文件路径上传到名为'factoryimages'的表。

这样做的最佳方式是什么?

我的控制器功能:

function do_upload()
{
    $config['upload_path'] = './assets/uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1000';
    $config['max_width']  = '';
    $config['max_height']  = '';
    $config['overwrite'] = TRUE;
    $config['remove_spaces'] = TRUE;

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

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

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

我在我的模型中试过这个,但它不起作用:

function insert_images()
{
    $insert_data = array(
    'filename' => $image_data['file_name'],
    'fullpath' => $image_data['full_path']
    );

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

我的观点:

<?php echo $error;?>
<br/>
<?php echo form_open_multipart('upload/do_upload');?>

<input type="file" name="userfile" size="20" multiple="true" />

<br /><br />

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

</form>

编辑:

我收到服务器错误,但我看不出它是什么。

我的控制器功能:

function do_upload()
{
    $config['upload_path'] = './assets/uploads/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size'] = '1000';
    $config['max_width']  = '';
    $config['max_height']  = '';
    $config['overwrite'] = TRUE;
    $config['remove_spaces'] = TRUE;

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

    if ( ! $this->upload->do_upload())
    {
        $error = array('error' => $this->upload->display_errors());
        $this->upload_model->insert_images($this->upload->data());
        $this->load->view('members/header');
        $this->load->view('members/upload_form', $error);
        $this->load->view('members/footer');
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
        $this->load->view('members/header');
        $this->load->view('members/upload_success', $data);
        $this->load->view('members/footer');
    }
}

我的模特功能:

function insert_images($data = array())
{
    $data = array(
    'filename' => $image_data['file_name'],
    'fullpath' => $image_data['full_path']
    );

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

可能是什么问题?

1 个答案:

答案 0 :(得分:3)

您必须将此数据作为数组$this->upload->data()

传递给您的插入内容

在您的控制器中,您必须在验证完成时进行设置

else
{
    $this->MODELNAME->insert_images($this->upload->data());
    $data = array('upload_data' => $this->upload->data());
    $this->load->view('members/header');
    $this->load->view('members/upload_success', $data);
    $this->load->view('members/footer');
}

在你的模特:

function insert_images($image_data = array()){
      $data = array(
          'filename' => $image_data['file_name'],
          'fullpath' => $image_data['full_path']
      );
      $this->db->insert('bedrijfimages', $data);
}

您可以在CI文档页面查看此数组中的信息:Codeigniter upload library