1

i am trying to upload multiple images using codeigniter. But images are not uploaded to the upload_images folder.

no errors are shown.

View form

    <form  action="<?php echo base_url(); ?>property/add_images2"  method="post" enctype="multipart/form-data">
        <input type="file" name="files1" multiple="multiple" accept="image/*">
        <div id="image">

        </div>
        <a id="another_image" href="#">Add Another Image</a>
        <input type="submit" value="Upload">
    </form>

jquery

<script type="text/javascript">

    $(document).ready(function(){
        var count = 2;
        $('#another_image').click (function(){

            if(count < 7){
            $('<input type="file" name="files'+count+'" multiple="multiple" accept="image/*">').appendTo ("#image");
            count++;
            }
        });
        });
    </script>

Controller

function add_images2(){

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

$files = $_FILES;

   if($files){
        for( $i = 1; $i < 7; $i++) {

         $config = array();
            $config['upload_path'] = './upload_images/';
            $config['allowed_types'] = 'gif|jpg|jpeg|png';
            $config['max_size']      = '10000';
            $config['file_name'] = 'prop_'.md5(time());
            $config['overwrite']     = FALSE;
            $this->load->library('upload', $config);

            $this->upload->do_upload('files'.$i);
        }
}

    $this->load->view('add_images2');

}

don't know where i made the mistake.

Tnx..

Sathya Baman
  • 3,424
  • 7
  • 44
  • 77
  • Possible duplicate of http://stackoverflow.com/questions/2788700/codeigniter-image-upload-not-working?rq=1 – Cristik Apr 19 '15 at 18:07
  • no that's for single image single image upload. i am trying for multiple images. with the same format. – Sathya Baman Apr 19 '15 at 18:10
  • Check path and the perms on upload dir – Mike Miller Apr 19 '15 at 18:17
  • permissions are good and the file path is also correct, i checked. – Sathya Baman Apr 19 '15 at 18:21
  • You need to set array element to name of input field i.e. `name="file[]"`. To dinamically create next field, use `id` attribute in JS code. Check [this](http://stackoverflow.com/questions/11524356/multiple-files-upload-array-with-codeigniter-2-0#answer-11539061) answer too. – Tpojka Apr 19 '15 at 19:17

1 Answers1

2

Write the below Function in your Controller.

/*It will upload the images to the specified path and will return the image urls in an array*/

private function upload_files($path, $files)
{
    $path = PHYSICAL_PATH . $path;
    $config = array(
        'upload_path'   => $path,
        'allowed_types' => 'JPG|JPEG|GIF|PNG|gif|jpg|png|jpeg|tft|TFT',
        'overwrite'     => 1,                       
    );

    $this->load->library('upload', $config);
    $images = array();      
    foreach ($files['name'] as $key => $image) {
        $_FILES['images[]']['name']= $files['name'][$key];
        $_FILES['images[]']['type']= $files['type'][$key];
        $_FILES['images[]']['tmp_name']= $files['tmp_name'][$key];
        $_FILES['images[]']['error']= $files['error'][$key];
        $_FILES['images[]']['size']= $files['size'][$key];

        $fileName = time() .'_'. $image;

        $images[] = $fileName;

        $config['file_name'] = $fileName;

        $this->upload->initialize($config);

        if ($this->upload->do_upload('images[]')) {
            $this->upload->data();
        } else {
            echo $this->upload->display_errors();die();
            return false;
        }
    }
    return $images;
}

call the above Function in your function

$firstFileName = $_FILES['photo']['name'][0];//Check whether any Images is uploaded or not.
if($firstFileName != "")
{   
    $path = "uploads/property_image/";//Give your Path name
    $values = $this->upload_files($path,$_FILES['photo']);
/* $values contents the Image path Array.Now you can send the array to the View Page for your use or can call any Models to store into the DB. */

}
  • Could you add an explanation of why this code fixes the problem? This will serve as reference for future users that will get on this page. – Cristik Apr 19 '15 at 18:50
  • The function upload_files is the custom function written by me , which is taking the FILE array which contents the all images information.Inside the foreach loop I am trying to upload the images one by one through the CI upload function do_upload().I am keeping all the image path in an array and returning the same for use.(Either to save in the DB or to use in the webpage. – Pravat Kumar Sahoo Apr 19 '15 at 19:03
  • Thanks, please also add this info to your answer, as it's much more visible than the comments area. – Cristik Apr 19 '15 at 19:04