使用Codeigniter多个图像上传
-
26-10-2019 - |
题
我的页面将插入用户名,ID和用户必须能够上传到10张图像。
IM的主要问题是使用CodeIgniter上传多个图像。
有人可以建议我如何将每个单独图像的每个图像路径传递到数组,以便我可以将其传递到数据库。
而且,如果用户选择要少于10张图像,例如2或5,那么我如何忽略错误的错误,即未选择文件,而JST仅通过已上传的图像。
<form method="post" action="uploader/go" enctype="multipart/form-data"> <input name="username" type="text" /><br /> <input name="nid" type="text" /><br /> <input type="file" name="image1" /><br /> <input type="file" name="image2" /><br /> <input type="file" name="image3" /><br /> <input type="file" name="image4" /><br /> <input type="file" name="image5" /><br /> <input type="file" name="image6" /><br /> <input type="file" name="image7" /><br /> <input type="file" name="image8" /><br /> <input type="file" name="image9" /><br /> <input type="file" name="image10" /><br /> <input type="submit" name="go" value="Upload!!!" /> </form>
样品控制器
$image_data=array('image_info' => $this->upload->data('image'));
$image_path=$image_data['image_info']['full_path'];
$data =array(
'id'=>uniqid('id_'),
'nID'=>$this->input->post('nid'),
'username'=>$username,
'image1'=>$image_path
}
任何帮助将不胜感激。
解决方案
将输入名称更改为:
<input type="file" name="image[1]" /><br />
<input type="file" name="image[2]" /><br />
<input type="file" name="image[3]" /><br />
而是 $this->upload->data('image')
将是一个数组。
然后您可以做:
foreach($this->upload->data('image') as $image) {
passItToTheDatabase(); // your custom function
}
不隶属于 StackOverflow