gpt4 book ai didi

php - 使用 CodeIgniter 上传多个图像

转载 作者:行者123 更新时间:2023-12-02 22:11:12 24 4
gpt4 key购买 nike

我是在项目中使用代码点火器的新用户,我在上传多个文件时遇到一个问题,但最后一个文件仅插入到所有图像中的三个图像字段。

我的 Controller 是:

function products()
{
date_default_timezone_set("Asia/Kolkata");
$config['upload_path'] = './resources/images/products/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$data = array('prod_image' => $this->upload->data(),
'prod_image1' => $this->upload->data(),
'prod_image2' => $this->upload->data());
$product_image=$data['prod_image']['file_name'];
$product_image1=$data['prod_image1']['file_name'];
$product_image2=$data['prod_image2']['file_name'];

$data = array(
'name' => $this->input->post('pd_name'),
'prod_image' => $product_image,
'prod_image1' => $product_image1,
'prod_image2' => $product_image2,
'created_time' => date('Y-m-d H:i:s'));

// insert form data into database
$result_set= $this->tbl_products_model->insertUser($data);

}

我的 View 部分是:

<input class="form-control" name="pd_name"type="text"/>
<input type="file" class="file_upload2" name="userfile"/> //1
<input type="file" class="file_upload2" name="userfile"/> //2
<input type="file" class="file_upload2" name="userfile"/>//3

请帮忙如何插入3张图片。

我的数据库就像

===========================================
id|name|prod_image|prod_image1|prod_image2|
===========================================
1|ard| | | |
============================================

最佳答案

HTML:

<input type="file" name="userfile[]" multiple="multiple">

PHP:

<?php
public function products()
{
$this->load->library('upload');
$dataInfo = array();
$files = $_FILES;
$cpt = count($_FILES['userfile']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['userfile']['name']= $files['userfile']['name'][$i];
$_FILES['userfile']['type']= $files['userfile']['type'][$i];
$_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
$_FILES['userfile']['error']= $files['userfile']['error'][$i];
$_FILES['userfile']['size']= $files['userfile']['size'][$i];

$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload('userfile');
$dataInfo[] = $this->upload->data();
}

$data = array(
'name' => $this->input->post('pd_name'),
'prod_image' => $dataInfo[0]['file_name'],
'prod_image1' => $dataInfo[1]['file_name'],
'prod_image2' => $dataInfo[2]['file_name'],
'created_time' => date('Y-m-d H:i:s')
);
$result_set = $this->tbl_products_model->insertUser($data);
}

private function set_upload_options()
{
//upload an image options
$config = array();
$config['upload_path'] = './resources/images/products/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['overwrite'] = FALSE;

return $config;
}
?>

关于php - 使用 CodeIgniter 上传多个图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40778683/

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