Welcome Guest, Not a member yet? Register   Sign In
Uploading multiple images
#1

[eluser]hema[/eluser]
Hi All,

Iam 1 month old in CI. Iam struck with uploading two multiple images at a time.When i submit the form two images should be uploaded. But it is not happening. Can anyone help me out.


This is my view file

<form method=post action="do_upload" enctype="multipart/form-data">

<table border=1 width=500 height=200>
<tr>
<td>Image1</td>
<td>&lt;Input type=file name="userfile"&gt;&lt;/td> // this file is uploading correctly. if i change userfile to //someother name it will not upoload. I dont no why it happens.
</tr>
<tr>
<td>Image2</td>
<td>&lt;Input type=file name="Image2"&gt;&lt;/td> // this file i am unable to upload
</tr>
</table>
&lt;/form&gt;

this is my controller


function do_upload()
{

$ImageName = "";


$config['upload_path'] = 'application/Pics/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//$config['max_size'] = '100';
$config['max_width'] = '1200';
$config['max_height'] = '850';

$this->load->library('upload', $config);



if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
//$this->load->view('addproduct', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
//$this->load->view('upload_success', $data);

foreach($data as $Key => $Value)
{
$ImageName="$Value[file_name]";
}

}


}


#2

[eluser]hema[/eluser]
No thanks, I resolved it.

Use this function if anyone wants to upload multiple images at a time
http://ellislab.com/forums/viewthread/71999/P10/
#3

[eluser]Mikhail Menshinskiy[/eluser]
It happens because you didn't specify a $field in the $this->upload->do_upload() method and CI load file with name 'userfile' by default.

Upload class:
Code:
public function do_upload($field = 'userfile')
{
...
}

ie. you should use:
Code:
$this->upload->do_upload('userfile');
...
$this->upload->do_upload('Image2');
#4

[eluser]toopay[/eluser]
You should validate each file, then upload each file :
Code:
function do_upload()
{
      $config = array(
                        'upload_path'   => 'application/Pics/',
                        'allowed_types' => 'gif|jpg|png|jpeg',
                        'max_width'     => '1200',
                        'max_height'    => '50',
      );

      $this->load->library('upload');

      if (!empty($_FILES['userfile']['name']))
      {
            $this->upload->initialize($config);
            if ($this->upload->do_upload('userfile'))
            {
                $data = $this->upload->data();
            }
            else
            {
                $error = array('error' => $this->upload->display_errors());
            }
      }
      
      if (!empty($_FILES['Image2']['name']))
      {
            $this->upload->initialize($config);
            if ($this->upload->do_upload('Image2'))
            {
                $data = $this->upload->data();
            }
            else
            {
                $error = array('error' => $this->upload->display_errors());
            }
      }
}

ps : use code tags when posting your code, it make easier to anyone else when read your code ;-)




Theme © iAndrew 2016 - Forum software by © MyBB