3

我知道这个问题已经被问过好几次了,但是我发现的所有其他问题都与我想要的不同。

我想将文件名和文件路径上传到名为“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);
}

可能是什么问题呢?

4

1 回答 1

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 上传库

于 2013-04-24T11:11:46.033 回答