gpt4 book ai didi

php - 使用 Codeigniter 上传多张图片,仅将一个文件路径保存到 MySQL 数据库

转载 作者:行者123 更新时间:2023-11-29 03:47:52 24 4
gpt4 key购买 nike

现在我正在构建一个应用程序。我已经设置了一个表单,可以将多个图像上传到数据库。这是我的简单代码

查看

<?php echo form_open_multipart('admin/product/post'); ?>
<table class="table table-stripped">
<tbody>
<tr>
<td>Code</td>
<td>
<?php echo form_input(array('class'=>'form-control','name'=>'kodeproduk')); ?>
</td>
</tr>
<tr>
<td>Display</td>
<td>
<input class="form-control" type="file" name="userfile[]" id="multiple" multiple="" />
</td>
</tr>
<tr>
<td>Description</td>
<td>
<div class="textarea textarea-editor">
<textarea name="ket" cols="50" rows="5" class="form-control"></textarea>
</div>
</td>
</tr>
<tr>
<td colspan="2">
<button type="submit" class="btn btn-primary btn-outline btn-block pull-right"><span>Save</span></button>
</td>
</tr>
</tbody>
</table>
<?php echo form_close(); ?>

Controller

public function post(){        
if($this->_validation()===FALSE){
$this->session->set_flashdata('error', 'Ooops, there was an error');
redirect(base_url("admin/product"));
}else{
$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();
$fileName = $_FILES['userfile']['name'];
$images[] = $fileName;
}
$fileName = implode(',',$images);

$data = array( 'kodeProduk' => $this->input->post('kodeproduk'),
'ket' => $this->input->post('ket'),

'GambarBesar' => $fileName
);

unset($data['submit']);
$this->table->add_record($data);
$this->session->set_flashdata('success', 'Product has been saved.');
redirect(base_url("admin/product"));
}
}

型号

public function add_record($data){
$this->db->insert('produk', $data);
return;
}

我在发布时遇到问题,所有图像文件都上传到服务器上的目录,但实际上只有一个图像作为一行存储到 MySQL 表中。那么如何修复我的代码呢?提前致谢

最佳答案

好的,一些小的改变可能会有所帮助

public function post(){        
if($this->_validation()===FALSE){
$this->session->set_flashdata('error', 'Ooops, there was an error');
redirect(base_url("admin/product"));
}else{
$files = $_FILES;
$images = array();
$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();
$images[] = $_FILES['userfile']['name'];
}
$fileName = implode(',',$images);

$data = array( 'kodeProduk' => $this->input->post('kodeproduk'),
'ket' => $this->input->post('ket'),

'GambarBesar' => $fileName
);

unset($data['submit']);
$this->table->add_record($data);
$this->session->set_flashdata('success', 'Product has been saved.');
redirect(base_url("admin/product"));
}
}

关于php - 使用 Codeigniter 上传多张图片,仅将一个文件路径保存到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35471811/

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