gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 08:20:04 24 4
gpt4 key购买 nike

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

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

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

我的 Controller 功能:

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>

编辑:

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

我的 Controller 功能:

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

可能是什么问题?

最佳答案

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

传递给您的插入

在您的 Controller 上,您必须在验证完成时进行设置

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

关于php - 如何将图片路径和名称上传到数据库 - Codeigniter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16190381/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com