How to upload image path and name to database - Codeigniter

大兔子大兔子 提交于 2019-12-10 10:25:14

问题


I know that this question has been asked several times, but all the other question I found are different from what I want.

I want to upload the filename and filepath to a table called 'factoryimages'.

What's the best way of doing this?

My controller function:

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');
    }
}

I tried this in my model, but it didn't work:

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

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

My view:

<?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>

Edit:

I get an server error but i can't see what it is.

My controller function:

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');
    }
}

My model function:

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

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

What could be the problem?


回答1:


You must pass this data to your insert as array $this->upload->data()

At your Controller you must set when validation is done

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');
}

And at your model:

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

You can check what information is inside this array at CI documentation page: Codeigniter upload library



来源:https://stackoverflow.com/questions/16190381/how-to-upload-image-path-and-name-to-database-codeigniter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!