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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16190381/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:40:58  来源:igfitidea点击:

How to upload image path and name to database - Codeigniter

phpmysqlcodeigniteruploadinsert

提问by Kees Sonnema

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'.

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

可能是什么问题呢?

采纳答案by Svetoslav

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

您必须将此数据作为数组传递给您的插入 $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

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