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
Step1
------------------------------------------
change html like this
<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="pimage[]">
<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="pimage[]">
<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="pimage[]">
<input type="hidden" name="pimage15p" value="<?=$val->floorplan15?>" />
step2
-----------
paste code in controller
// Controller: Upload.php
class Upload extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index() {
$this->load->view('upload_form', array('error' => ''));
}
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000; // Adjust as per your need
$this->load->library('upload', $config);
$upload_data = array();
// Loop through each file input
for ($i = 1; $i <= 15; $i++) {
if (!empty($_FILES['pimage']['name'][$i - 1])) {
$_FILES['userfile']['name'] = $_FILES['pimage']['name'][$i - 1];
$_FILES['userfile']['type'] = $_FILES['pimage']['type'][$i - 1];
$_FILES['userfile']['tmp_name'] = $_FILES['pimage']['tmp_name'][$i - 1];
$_FILES['userfile']['error'] = $_FILES['pimage']['error'][$i - 1];
$_FILES['userfile']['size'] = $_FILES['pimage']['size'][$i - 1];
if ($this->upload->do_upload('userfile')) {
$upload_data[$i] = $this->upload->data();
} else {
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
return;
}
}
}
// Handle uploaded data (e.g., save to database)
// Example: You can access uploaded file paths like $upload_data[1]['file_name'], $upload_data[2]['file_name'], etc.
$this->load->view('upload_success', array('upload_data' => $upload_data));
}
}