Upload multiple images with codeigniter

SdR

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?

Linus

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to upload multiple images using codeigniter?

From Dev

how to upload multiple images on codeigniter from single input fileds

From Dev

Multiple files upload in Codeigniter

From Dev

multiple file upload in codeigniter

From Dev

Codeigniter Multiple upload files

From Dev

Uploading multiple images in codeigniter?

From Dev

Uploading multiple images in codeigniter?

From Dev

Upload multiple images

From Dev

Prevent selecting multiple images and Select only single image in image upload function in codeigniter

From Dev

How do I upload multiple images and text file in a single form with the different button in Codeigniter?

From Dev

Multiple file upload with Codeigniter 3

From Dev

CodeIgniter Multiple File Upload not working

From Dev

CodeIgniter multiple image upload and resize

From Dev

How to upload multiple file in codeigniter?

From Dev

codeigniter form upload with images using ajax

From Dev

How to upload 2 separate images in codeigniter

From Dev

Upload images by setting specific size with codeigniter

From Dev

How to set upload images limit in codeigniter 3

From Dev

Upload multiple images using AFNetworking

From Dev

Upload multiple images to Database MYSQLI

From Dev

Upload multiple images in one request

From Dev

Multiple Upload Images into Mysql Database

From Dev

upload Multiple images to desired folder

From Dev

Upload multiple images with titles and descriptions

From Dev

Upload multiple images to Database MYSQLI

From Dev

Validate multiple upload images with django

From Dev

Multiple Images upload with different titles

From Dev

Unable to upload multiple images with Carrierwave

From Dev

All images not uploading in multiple images upload

Related Related

HotTag

Archive