现在我正在构建一个应用程序。我已经设置了一个将多个图像上传到数据库的表单。这是我的简单代码
查看
<?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表中。那么如何修复我的代码呢?谢谢提前
答案 0 :(得分:0)
好的一些小改动可能会有所帮助
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"));
}
}
答案 1 :(得分:-1)
请尝试此代码我希望这是解决
ArrayAdapter