Friday, 24 June 2016

Upload Multiple Images in Codeigniter

1.Html Form

 <html>
 <head>
    <title>Upload Form</title>
</head>
<body>
<?php echo $error;?>
<?php echo form_open_multipart('photos/image_upload');?>
<input type="file" multiple name="userfile[]" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
</body>
</html>

2.Controller Code

 function file_upload()
{      
    $this->load->library('upload');

    $files = $_FILES;
    $cpt = count($_FILES['userfile']['name']);
    for($i=0; $i<$cpt; $i++)
    {          
        $_FILES['userfile']['name']= $files['userfile']['name'][$i];
        $_FILES['userfile']['type']= $files['userfile']['type'][$i];
        $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
        $_FILES['userfile']['error']= $files['userfile']['error'][$i];
        $_FILES['userfile']['size']= $files['userfile']['size'][$i];  
        $this->upload->initialize($this->upload_config());
        $this->upload->do_upload();
    }
}

private function upload_config()
{  
    //upload an image options
    $config = array();
    $config['upload_path'] = './Images/';
    $config['allowed_types'] = 'gif|jpg|png|jpeg';
    $config['max_size']      = '0';
    $config['overwrite']     = FALSE;
    return $config;
}

No comments:

Post a Comment