[eluser]brian88[/eluser]
Here is a function that uploads a bunch of images. Everything works fine besides a bug in the image manipulation where i highlighted the code below.
There are 8 file uploads. So users can upload 8 images and then i want to resize each one of those images.
Take out the image manipulation and all the images upload just fine. But with the highlighted code below it only uploads the first image and resizes it. doesnt do anymore uploads
Code:
$data['num_of_images'] = 8;
// if user presses submit button
if($this->input->post()){
// get the name of the truck
$db_data['name'] = $this->input->post('name');
// format the truck name to be a folder name
$folder = str_replace( " ", "", strtolower($this->input->post('name')) );
// if the name of the truck folder does not currently exist.
if(!is_dir('assets/images/'.$folder)){
// make a folder based off the name of the truck
mkdir('assets/images/'.$folder, 0777);
// config for file uploads
$config['upload_path'] = FCPATH . 'assets/images/' . $folder;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
// upload users submitted images
for ($i=1; $i<=$data['num_of_images']; $i++){
// change file name in folder
$config['file_name'] = $folder . '-' . $i;
$this->upload->initialize($config);
// if user uploaded an image
if ( !empty($_FILES["file" . $i]['name']) ){
// upload file
if($this->upload->do_upload("file".$i)){
$upload_data = $this->upload->data();
// store name for database
$db_data['image'.$i] = $folder.'/'.$upload_data['file_name'];
// error: resize only image happens for the first image, and only 1st image uploads
// -----------------------------------
if( $this->input->post('dontResize') != 'true'){
$config = array(
'source_image' => $upload_data['full_path'],
'maintain_ration' => true,
'create_thumb' => true,
'master_dim' => 'width',
'quality' => 60,
'width' => 800,
'height' => 600
);
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
// -----------------------------------
// end resize
}else{ // error
echo $this->upload->display_errors();
} // end if do_upload
} // end if empty file upload
} // end for each image
// add to database
$this->main_mod->addTruck($db_data);
// refresh the page to see view new data added.
redirect('/');
}else{
// The truck name folder exist. Need a new name for the folder.
echo 'Error: This truck name already exist. Choose a different name.';
}