在编码点火器中批量上传图像



我是Codeigniter的新手,我正在尝试使用loop更新多个图像。但我无法这样做,因为如果有 1 条记录,foreach loop只更新 3 条记录。提前谢谢。

控制器.php

if(!empty($_FILES['product_image']['name'][0])) {
$number_of_files = sizeof($_FILES['product_image']['tmp_name']);
$files = $_FILES['product_image'];
$config = array(
'upload_path' => FCPATH.'uploads/product_images/',
'allowed_types' => 'jpg|png|jpeg'
);
for($i=0;$i<$number_of_files;$i++) {
$_FILES['product_image']['name'] = $files['name'][$i];
$_FILES['product_image']['type'] = $files['type'][$i];
$_FILES['product_image']['tmp_name'] = $files['tmp_name'][$i];
$_FILES['product_image']['error'] = $files['error'][$i];
$_FILES['product_image']['size'] = $files['size'][$i];
$this->upload->initialize($config);
if($this->upload->do_upload('product_image')) {
$imgData = $this->upload->data();
}
$img[] = array(
'Image' => $files['name'][$i],
'SortOrder' => $_POST['sort_order'][$i]
);
//unlink("uploads/product_images/".$_POST['path']);
}
if($query = $this->M_Product->editProductImg($img,$prodId)) {
//$error = 0;
print_r($query);
} else {
//$error = 1;
}
}

型号.php

public function editProductImg($img,$prodId) {
$checkExist = $this->fetchSingleImage($prodId);
if(empty($checkExist)) {
if($this->db->insert_batch('tbl_product_images',$img)) {
return true;
} else {
return false;
}
} else {
foreach($img as $key => $value) {  
$this->db->query("update tbl_product_images SET Image='".$value['Image']."',SortOrder='".$value['SortOrder']."' WHERE ProductId='".$prodId."'");
return $this->db->last_query();
}
}
}

请检查计算的文件数是否正确或尝试使用

$number_of_files= count($_FILES);

同时将第一个"如果"更改为

if(!empty($_FILES))

相关内容

  • 没有找到相关文章

最新更新