Upload multiple images with codeigniter

2019-01-09 19:06发布

问题:

I have used some code I found before to upload 1 image at a time and this works perfect. I would like to use the same style of code but for multiple images.

The Model:

 function cover_upload($afbeelding) {
    // path where the picture needs to be uploaded
    echo $album_path = APPPATH . 'images/covers';
    // configuration for the upload
    $ext = end(explode(".", $_FILES['userfile']['name']));
    $config = array(
        'file_name' => $afbeelding . '.' . $ext,
        'upload_path' => $album_path,
        'allowed_types' => 'gif|jpg|jpeg|png',
        'max_size' => '5120'
    );
    // load upload library with the configuration
    $this->load->library('upload', $config);
    // upload picture with the upload library
    if (!$this->upload->do_upload()) {
        echo $this->upload->display_errors();
        die();
    }
    // get data array of the uploaded picture
    $image_data = $this->upload->data();
    // configuration for the picture thumbnail resize
    echo $image_data['full_path'];
    $config = array(
        'source_image' => $image_data['full_path'],
        'new_image' => realpath($album_path . '/thumb'),
        'maintain_ration' => TRUE,
        'width' => 300,
        'height' => 300
    );
    // load image manupulation library with the configuration
    $this->load->library('image_lib', $config);
    // resize picture
    $this->image_lib->resize();
    $this->image_lib->clear();
    // submit file name of the uploaded picture to save it in the database
    $bestandsnaam = $image_data['file_name'];
    return $bestandsnaam;
}

the view (just the part about the image):

<div class="form-group">
            <label class="col-sm-2 control-label" for="CoverFoto">picture</label>
            <div class="col-sm-5"> 
                <div class="fileinput fileinput-new" data-provides="fileinput">
                    <div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px;"></div>
                    <div>
                        <span class="btn btn-default btn-file"><span class="fileinput-new">select_an_image</span><span class="fileinput-exists">change</span><input type="file" name="userfile"></span>
                        <a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput"><?php echo $this->lang->line("remove"); ?></a>
                    </div>
                </div>
            </div>
        </div>

And the controller:

if (!empty($_FILES['userfile']['name'])) {
            $gamma->CoverFoto = $this->gamma_model->cover_upload(url_title($gamma->Naam, '_', true));
        }

Is there a way to use this code so i can upload multiple images?

回答1:

Have a look upon this code may this help in understanding you how to handle multiple images

       #####################
        # Uploading multiple# 
        #     Images        #
        #####################


        $files = $_FILES;
        $count = count($_FILES['uploadfile']['name']);
        for($i=0; $i<$count; $i++)
                {
                $_FILES['uploadfile']['name']= $files['uploadfile']['name'][$i];
                $_FILES['uploadfile']['type']= $files['uploadfile']['type'][$i];
                $_FILES['uploadfile']['tmp_name']= $files['uploadfile']['tmp_name'][$i];
                $_FILES['uploadfile']['error']= $files['uploadfile']['error'][$i];
                $_FILES['uploadfile']['size']= $files['uploadfile']['size'][$i];
                $this->upload->initialize($this->set_upload_options());//function defination below
                $this->upload->do_upload('uploadfile');
                $upload_data = $this->upload->data();
                $name_array[] = $upload_data['file_name'];
                $fileName = $upload_data['file_name'];
                $images[] = $fileName;

                }
              $fileName = $images;

what's happening in code??

well $_FILE---->it is an associative array of items uploaded to the current script via the POST method.for further look this LINK

it's an automatic variable avaliable within all scopes of script

 function set_upload_options()
  { 
  // upload an image options
         $config = array();
         $config['upload_path'] = LARGEPATH; //give the path to upload the image in folder
         $config['remove_spaces']=TRUE;
         $config['encrypt_name'] = TRUE; // for encrypting the name
         $config['allowed_types'] = 'gif|jpg|png';
         $config['max_size'] = '78000';
         $config['overwrite'] = FALSE;
         return $config;
  }

and in your html markup don't don't forget:

  1. Input name must be be defined as an array i.e. name="file[]"
  2. Input element must have multiple="multiple" or just multiple

    3.$this->load->library('upload'); //to load library

    4.The callback, $this->upload->do_upload() will upload the file selected in the given field name to the destination folder.

    5.And the callback $this->upload->data() returns an array of data related to the uploaded file like the file name, path, size etc.