i am trying to upload multiple images using codeigniter, i have around 15 image fields with the name pimage to pimage15, all are optional, my controller code is like below:
public function editprojectplans() {
$data = array();
if ($this->isUserLoggedIn) {
$con = array(
'id' => $this->session->userdata('userId')
);
$data['user'] = $this->user->getRows($con);
$id = $this->uri->segment(3);
$data['vals'] = $this->category->selectproject($id);
if (isset($_POST['editproduct'])) {
// Check whether user uploaded pictures
$uploadPath = './uploads/products/';
$width = 1000; // Define width as per your requirement
$height = 1000; // Define height as per your requirement
for ($i = 1; $i <= 15; $i++) {
$fileInputName = 'pimage'.$i;
if (!empty($_FILES[$fileInputName]['name'])) {
$uploadImgData[$i] = $this->processImageUpload($fileInputName, $uploadPath, $width, $height);
}
}
...........................
private function processImageUpload($field, $uploadPath, $width, $height) {
$this->load->library('upload');
$this->load->library('image_lib');
$imageData = array();
$ImageCount = count($_FILES[$field]['name']);
for ($i = 0; $i < $ImageCount; $i++) {
$_FILES['file']['name'] = $_FILES[$field]['name'][$i];
$_FILES['file']['type'] = $_FILES[$field]['type'][$i];
$_FILES['file']['tmp_name'] = $_FILES[$field]['tmp_name'][$i];
$_FILES['file']['error'] = $_FILES[$field]['error'][$i];
$_FILES['file']['size'] = $_FILES[$field]['size'][$i];
// File upload configuration
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$this->upload->initialize($config);
// Upload file to server
if ($this->upload->do_upload('file')) {
// Uploaded file data
$imageData[] = $this->upload->data();
// Resize uploaded image
$path = $imageData[$i]['full_path'];
$config['image_library'] = 'gd2';
$config['source_image'] = $path;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
} else {
// Handle upload error
$error = array('error' => $this->upload->display_errors());
// You can log or handle the error as per your application's requirement
}
}
return $imageData;
}
my html is like:
<form action="" method="post" enctype="multipart/form-data" id="myform">
<input type="file" accept="image/png, image/gif, image/jpeg" class="w-full px-4 py-2 placeholder-secondary-400 border dark:bg-dark-card dark:border-secondary-800 rounded outline-none focus:border-primary-500 dark:focus:border-primary-500 focus:shadow"
name="pimage[]">
<input type="hidden" name="pimage1p" value="<?=$val->floorplan1?>" />
<input type="file" accept="image/png, image/gif, image/jpeg" class="w-full px-4 py-2 placeholder-secondary-400 border dark:bg-dark-card dark:border-secondary-800 rounded outline-none focus:border-primary-500 dark:focus:border-primary-500 focus:shadow"
name="pimage2[]">
<input type="hidden" name="pimage2p" value="<?=$val->floorplan2?>" />
<input type="file" accept="image/png, image/gif, image/jpeg" class="w-full px-4 py-2 placeholder-secondary-400 border dark:bg-dark-card dark:border-secondary-800 rounded outline-none focus:border-primary-500 dark:focus:border-primary-500 focus:shadow"
name="pimage3[]">
<input type="hidden" name="pimage3p" value="<?=$val->floorplan3?>" /> ...............
<input type="file" accept="image/png, image/gif, image/jpeg" class="w-full px-4 py-2 placeholder-secondary-400 border dark:bg-dark-card dark:border-secondary-800 rounded outline-none focus:border-primary-500 dark:focus:border-primary-500 focus:shadow"
name="pimage15[]">
<input type="hidden" name="pimage15p" value="<?=$val->floorplan15?>" />
here i am not getting any error and the file is not getting uploaded too, permisions are fine with server, limit and all is good, but there is some issue here, thanks in advance