Reputation: 6976
I am trying to upload 3 images and resize them each time I upload an image I am putting the image name in to an array images
, however at the end of the method, when I print_r($images)
I am only getting the name of the last image uploaded, can anyone tell me why this is?
Here is my code,
// lets get rid of any old configs
$secondaryConfig['upload_path'] = './media/uploads/news/';
$secondaryConfig['allowed_types'] = 'gif|jpg|png';
$secondaryConfig['max_size'] = '4000';
$secondaryConfig['max_width'] = '230';
$secondaryConfig['max_height'] = '120';
$secondaryManipulationConfig['image_library'] = 'gd2';
$secondaryManipulationConfig['create_thumb'] = FALSE;
$secondaryManipulationConfig['maintain_ratio'] = TRUE;
$secondaryManipulationConfig['width'] = 132;
$secondaryManipulationConfig['height'] = 80;
//image_1
$this->upload->initialize($secondaryConfig);
if (!$this->upload->do_upload('image_1'))
{
$data['error_image_1'] = $this->upload->display_errors();
$this->template->build('admin/news/edit', $data);
return;
}
else
{
$image = $this->upload->data();
$images[] = $image['file_name'];
$this->image_lib->initialize($secondaryManipulationConfig);
$secondaryManipulationConfig['source_image'] = './media/uploads/news/'.$image['file_name'];
if (!$this->image_lib->resize())
{
$data['error_image_1'] = $this->image_lib->display_errors();
$this->template->build('admin/news/edit', $data);
return;
}
}
$this->upload->initialize($secondaryConfig);
if (!$this->upload->do_upload('image_2'))
{
$data['error_image_2'] = $this->upload->display_errors();
$this->template->build('admin/news/edit', $data);
return;
}
else
{
$image = $this->upload->data();
$images[] = $image['file_name'];
$this->image_lib->initialize($secondaryManipulationConfig);
$secondaryManipulationConfig['source_image'] = './media/uploads/news/'.$image['file_name'];
if (!$this->image_lib->resize())
{
$data['error_image_2'] = $this->image_lib->display_errors();
$this->template->build('admin/news/edit', $data);
return;
}
}
//image_3
$this->upload->initialize($secondaryConfig);
if (!$this->upload->do_upload('image_3'))
{
$data['error_image_3'] = $this->upload->display_errors();
$this->template->build('admin/news/edit', $data);
return;
}
else
{
$image = $this->upload->data();
$images[] = $image['file_name'];
$this->image_lib->initialize($secondaryManipulationConfig);
$secondaryManipulationConfig['source_image'] = './media/uploads/news/'.$image['file_name'];
if (!$this->image_lib->resize())
{
$data['error_image_3'] = $this->image_lib->display_errors();
$this->template->build('admin/news/edit', $data);
return;
}
}
Upvotes: 0
Views: 611
Reputation: 1815
Buddy I'm not sure how you have written the HTML but try below sample code. you can create one test.php file to check the demo and then you can replicate logic in your code as per your need.
<?php
if($_POST)
{
print "<pre>";print_r($_FILES);print "</pre>";
}
?>
<form name="frm" method="post" enctype="multipart/form-data">
File1 : <input type="file" name="images[]" id="images[]"/> <br>
File2 : <input type="file" name="images[]" id="images[]"/> <br>
File3 : <input type="file" name="images[]" id="images[]"/> <br>
<input type="submit" name="Submit" />
</form>
and output will be like
Array
(
[images] => Array
(
[name] => Array
(
[0] => Chrysanthemum.jpg
[1] => Desert.jpg
[2] => Tulips.jpg
)
[type] => Array
(
[0] => image/jpeg
[1] => image/jpeg
[2] => image/jpeg
)
[tmp_name] => Array
(
[0] => C:\wamp\tmp\phpE961.tmp
[1] => C:\wamp\tmp\phpE971.tmp
[2] => C:\wamp\tmp\phpE972.tmp
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[size] => Array
(
[0] => 879394
[1] => 845941
[2] => 620888
)
)
)
Upvotes: 1