jquery - 如何在codeigniter中使用jquery ajax插入多个图像名称并移动到文件夹中?
问题描述
在我的代码中,我有一个输入字段<input type="file" id="product_image" name="product_image[]" multiple>
,当我们将图像移动到文件夹中时,我想将多个图像名称插入到我的数据库中。
当我使用var_dump($_FILES)
它时告诉我
array(1) { ["product_image"]=> array(5) { ["name"]=> array(1) { [0]=> string(9) "img3.jpeg" } ["type"]=> array(1) { [0]=> string(10) "image/jpeg" } ["tmp_name"]=> array(1) { [0]=> string(14) "/tmp/phpd6jSmA" } ["error"]=> array(1) { [0]=> int(0) } ["size"]=> array(1) { [0]=> int(40711) } } }
看法:
<script>
$(document).ready(function(){
$("#submit").click(function(e){
e.preventDefault();
product_name = $("#product_name").val();
quantity = $("#quantity").val();
var formData = new FormData();
$.each($("#product_image"), function (i, obj) {
$.each(obj.files, function (j, file) {
formData.append('product_image[' + i + ']', file);
});
});
formData.append('product_name', product_name);
formData.append('quantity', quantity);
$.ajax({
type:"POST",
data:formData,
processData: false,
contentType: false,
url:"<?php echo base_url(); ?>admin/products",
success:function(data){
$("#success_upload").html(data);
}
});
});
});
</script>
<input type="text" class="form-control" id="product_name" name="product_name">
<input type="text" class="form-control" id="quantity" name="quantity">
<input type="file" id="product_image" name="product_image[]" multiple>
<input type="submit" class="btn btn-primary" id="submit" name="submit">
控制器:
public function products()
{
$dataInfo = array();
$files = $_FILES;
$cpt = count($_FILES['product_image']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['product_image']['name']= $files['product_image']['name'][$i];
$_FILES['product_image']['type']= $files['product_image']['type'][$i];
$_FILES['product_image']['tmp_name']= $files['product_image']['tmp_name'][$i];
$_FILES['product_image']['error']= $files['product_image']['error'][$i];
$_FILES['product_image']['size']= $files['product_image']['size'][$i];
$this->upload->initialize($this->set_upload_options());
$this->upload->do_upload('product_image');
$dataInfo[] = $this->upload->data();
}
$data = array(
'product_name' => $this->input->post('product_name'),
'quantity' => $this->input->post('quantity'),
'product_image' => implode(",",array_column($dataInfo, 'product_image')),
);
$sql = $this->db->insert('add_product',$data);
if($sql == true)
{
echo '<p style="color:green;">New Product Added</p>';
}
else
{
echo '<p style="color:red;">Unable to Proceed!</p>';
}
}
private function set_upload_options()
{
$config = array();
$config['upload_path'] = ''.base_url().'resource/product/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1024';
$config['overwrite'] = FALSE;
return $config;
}
现在,只product_name and quantity
存储在数据库中,但我也想存储product_image
到我的数据库中,用逗号 (,) 分隔,比如img1.png,img2.png
. 请帮我。
谢谢你
解决方案
首先,检查图像是否正在上传。
如果上传,则将图像的名称推送到数组中。
public function products()
{
$images_name = array();
$files = $_FILES;
$cpt = count($_FILES['product_image']['name']);
for($i=0; $i<$cpt; $i++)
{
$_FILES['product_image']['name']= $files['product_image']['name'][$i];
$_FILES['product_image']['type']= $files['product_image']['type'][$i];
$_FILES['product_image']['tmp_name']= $files['product_image']['tmp_name'][$i];
$_FILES['product_image']['error']= $files['product_image']['error'][$i];
$_FILES['product_image']['size']= $files['product_image']['size'][$i];
$this->upload->initialize($this->set_upload_options());
if($this->upload->do_upload('product_image'))
{
$dataInfo = $this->upload->data();
array_push($images_name, $dataInfo['file_name']);
}
}
$data = array(
'product_name' => $this->input->post('product_name'),
'quantity' => $this->input->post('quantity'),
'product_image' => implode(",", $images_name)
);
$sql = $this->db->insert('add_product', $data);
if($sql)
{
echo '<p style="color:green;">New Product Added</p>';
}
else
{
echo '<p style="color:red;">Unable to Proceed!</p>';
}
}
推荐阅读
- python - Numpy 继承;向 Numpy 数组添加方法
- statsmodels - statsmodels ARIMA 预测没有外生变量的未来值
- azure - 如何将单个应用服务计划中的 Azure 应用服务分配给虚拟网络中的不同子网
- php - 口音显示正确的一半时间
- authentication - 在flutter中通过rest api登录验证用户
- javascript - Microsoft Websocket 代码示例返回错误
- discord.js - Discord.JS 阻止一个人写消息
- python - 如何通过 Selenium 查找动态 ID 的元素
- javascript - 需要帮助来理解在数组上使用 reduce() 的 react-native 代码
- javascript - 从文本文件中读取数据并在页面上填充输入的好方法