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;
}
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
New contributor
jbedufly app is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.