gpt4 book ai didi

php - 如何在codeigniter中发送多个文件上传?

转载 作者:行者123 更新时间:2023-11-29 15:47:37 25 4
gpt4 key购买 nike

我想插入多个文件的数据,但如果插入数据库,文件为空。这是我的代码

来自操作代码


<form action="<?php echo base_url();?>SystemCore/tambahKendaraan" method="POST" enctype="multipart/form-data">
<div class="form-group">
<label>Foto Foto Kendaraan</label>
<input type="file" class="form-control" multiple="" required="" name="addKendaraanDinasFoto[]">
<small>*Dapat Mencantumkan Foto Lebih Dari 1</small>
</div>
<button type="submit" class="btn btn-success">Simpan</button>
</form>

这是我上传数据的 Controller


public function tambahKendaraan()
{
$simpanData = array(
"id_jenis" => $this->input->post("addKendaraanDinasJenis"),
"plat_kendaraan" => $this->input->post("addKendaraanDinasPlat"),
"dinas_pengelola" => $this->input->post("addKendaraanDinasPengelola"),
"pemegang_kendaraan" => $this->input->post("addKendaraanDinasPemegang"),
"no_stnk" => $this->input->post("addKendaraanDinasSTNK"),
"no_rangka" => $this->input->post("addKendaraanDinasRangka"),
"masa_pajak" => $this->input->post("addKendaraanDinasPajak"),
"merk_kendaraan" => $this->input->post("addKendaraanDinasMerk"),
"warna_kendaraan" => $this->input->post("addKendaraanDinasWarna"),
"status_kendaraan" => $this->input->post("addKendaraanDinasStatus"),
);
if ($this->db->insert("tbl_mobil", $simpanData)) {

$insert_id = $this->db->insert_id();
date_default_timezone_get("Asia/Jakarta");

$this->load->library('upload');
$dataInfo = array();
$files = $_FILES;
$cpt = count($_FILES['addKendaraanDinasFoto']['name']);

for($i=0; $i<$cpt; $i++)
{
$_FILES['addKendaraanDinasFoto']['name']= $files['addKendaraanDinasFoto']['name'][$i];
$_FILES['addKendaraanDinasFoto']['type']= $files['addKendaraanDinasFoto']['type'][$i];
$_FILES['addKendaraanDinasFoto']['tmp_name']= $files['addKendaraanDinasFoto']['tmp_name'][$i];
$_FILES['addKendaraanDinasFoto']['error']= $files['addKendaraanDinasFoto']['error'][$i];
$_FILES['addKendaraanDinasFoto']['size']= $files['addKendaraanDinasFoto']['size'][$i];

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

$this->db->insert("tbl_fotomobil", $data);
}


foreach ($dataInfo as $infos) {
$data = array(
'id_mobil' => $insert_id,
'tgl_foto' => date('Y-m-d H:i:s'),
'foto_mobil' => $infos['file_name'],
);
}

$this->session->set_flashdata('notif', '<script>swal ( "Success" , "Data Kendaraan Berhasil Ditambahkan !" , "success" );</script>');
}else{
$this->session->set_flashdata('notif', '<script>swal ( "Error" , "Data Error !" , "error" );</script>');
}
redirect('kendaraan');
}
private function set_upload_options()
{
$config = array();
$config['upload_path'] = './upload/kendaraan/';
$config['allowed_types'] = 'jpg|png|jpeg|svg';
$config['max_size'] = 10097152;
$config['overwrite'] = FALSE;

return $config;
}

结果:

    array(3) {      ["id_mobil"]=>      int(0)      ["tgl_foto"]=>      string(19) "2019-07-09 09:55:58"      ["foto_mobil"]=>      string(0) ""    }    array(3) {      ["id_mobil"]=>      int(0)      ["tgl_foto"]=>      string(19) "2019-07-09 09:55:58"      ["foto_mobil"]=>      string(0) ""    }

最佳答案

  Controller ::: 

public function addmember()
{

if (empty($_FILES['user_image']['name']))
{
$this->form_validation->set_rules('user_image', 'User Image', 'required');
}
if (empty($_FILES['user_gallery']['name']))
{
$this->form_validation->set_rules('user_gallery[]', 'User Gallery', 'required');
}

if($this->form_validation->run() == FALSE)
{

$this->load->view('common_page/header');
$this->load->view('common_page/sidebar');
$this->load->view('form');
$this->load->view('common_page/footer');
}
else
{

$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);

if($this->upload->do_upload('user_image') && !empty($_FILES['user_gallery']['name']))
{
$uploadData = $this->upload->data();
$uploadedFile = $uploadData['file_name'];

$dataInfo = [];
$files = $_FILES;
$cpt = count($_FILES['user_gallery']['name']);

for($i=0; $i<$cpt; $i++)
{
$_FILES['user_gallery']['name'] = $files['user_gallery']['name'][$i];
$_FILES['user_gallery']['type'] = $files['user_gallery']['type'][$i];
$_FILES['user_gallery']['tmp_name'] = $files['user_gallery']['tmp_name'][$i];
$_FILES['user_gallery']['error'] = $files['user_gallery']['error'][$i];
$_FILES['user_gallery']['size'] = $files['user_gallery']['size'][$i];

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

$all_imgs = '';

if ( count($dataInfo) > 0) {

foreach ($dataInfo as $info) {

$all_imgs .= $info['file_name'];
$all_imgs .= ',';
}
}

$data = [
'user_image' => $uploadedFile,
'user_gallery' => $all_imgs
];

$this->Admin_model->insertData($data,'member');
$this->session->set_flashdata('success', 'Member Register Successfully !!!');
redirect('all/form');
}
else
{

}
}
}


VIEW :::
<form role="form" id="member_form" action="<?php echo
base_url('all/addmember');?>" method="post" enctype="multipart/form-data">
<div class="box-body">
<div class="form-group">
<label for="exampleInputFile">User Image</label>
<input type="file" id="file" name="user_image">
<?php echo form_error('user_image', '<div class="error">', '</div>'); ?>

</div>

<div class="form-group">
<label for="exampleInputFile">User Gallery Image</label>
<input type="file" id="gallery" name="user_gallery[]" multiple="">
<?php echo form_error('user_gallery', '<div class="error">', '</div>'); ?>
</div>
<div class="box-footer">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>

关于php - 如何在codeigniter中发送多个文件上传?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56948091/

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