使用Codeigniter上载多个图像仅将一个文件路径保存到MySQL数据库 [英] Multiple image upload with Codeigniter saving only one file path to MySQL Database
本文介绍了使用Codeigniter上载多个图像仅将一个文件路径保存到MySQL数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
现在,我正在构建一个应用程序。我已经建立了一个表格,可以将多个图像上传到数据库。这是我的简单代码
Now I'm building an application. I have set up a form that will upload multiple image to database. This is my simple code
查看
<?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(); ?>
控制器
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表中。那么如何修复我的代码?谢谢您提前
And I have a problem when I'm post it, all of the image files upload to the directory on the server, but only ONE of the images actually stores into the MySQL table as a row. So how to fixed my code ? Thanks advance
推荐答案
好的一些小的更改可能会有所帮助
Okay a few minor changes might help
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"));
}
}
这篇关于使用Codeigniter上载多个图像仅将一个文件路径保存到MySQL数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文