naseeba c
naseeba c

Reputation: 1050

How to upload multiple files in codeigniter 3.0.1

How to upload multiple files in codeigniter 3.0.1. There are similar issues and solutions in stackoverflow but unfortunately non of them are helping to fix the issue I am facing.

This is the error message appearing You did not select a file to upload with my current code

view (addGallery)

<section>
    <h2>Add Gallery</h2>
        <?php echo form_open('Newsupload/gallery', ['id'=>'news', 'name'=>'news', 'method'=>'post','enctype'=>'multipart/form-data']) ?>

        <div class="grp width-50">
            <label for="name">Album Name</label>
            <input type="text" name="name" id="name" value="" placeholder="">
        </div>
        <div class="grp width-100">
            <div id="selectedFiles"></div>
            <input type="file" id="files" name="files[]" multiple size="20"><br/>
        </div>
        <?php if (isset($error)) {
            echo $error;
        } ?>
        <grp class="grp width-100">
            <button>Add</button>
        </grp>
    </form>
</section>

controller (gallery)

public function gallery()
{

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

    $files = $_FILES;
    $cpt = count($_FILES['files']['name']);
    for($i=0; $i<$cpt; $i++)
    {           
        $_FILES['files']['name']= $files['files']['name'][$i];
        $_FILES['files']['type']= $files['files']['type'][$i];
        $_FILES['files']['tmp_name']= $files['files']['tmp_name'][$i];
        $_FILES['files']['error']= $files['files']['error'][$i];
        $_FILES['files']['size']= $files['files']['size'][$i];    

        $this->upload->initialize($this->set_upload_options());
        // $this->upload->do_upload('files[]');
        if (!$this->upload->do_upload('files[]'))
        {  
            $error =['error' => $this->upload->display_errors()];
            $this->load->view('admin/addGallery', $error);
        }
    }
}
public function set_upload_options()
{
    $config['upload_path'] = getcwd().'/upload/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['remove_spaces'] = true;
    return $config;
}

Upvotes: 12

Views: 19954

Answers (7)

Sandeep Pujare
Sandeep Pujare

Reputation: 147

Just that i understood from the library $this->upload->do_upload('inputelementname') doesn't have to be input element name. It should be the key of files array.

So in this context this should work

if($_FILES){
              $filecount=count($_FILES['addmediaelement']['name']);
              for($i=0; $i<$filecount; $i++){
                  $_FILES['mediaelement']['name']=$_FILES['addmediaelement']['name'][$i];
                  $_FILES['mediaelement']['type']=$_FILES['addmediaelement']['type'][$i];
                  $_FILES['mediaelement']['tmp_name']=$_FILES['addmediaelement']['tmp_name'][$i];
                  $_FILES['mediaelement']['error']=$_FILES['addmediaelement']['error'][$i];
                  $_FILES['mediaelement']['size']=$_FILES['addmediaelement']['size'][$i];

                  $config['upload_path']=path/to/save/file;
                  $config['file_name']=filealternatename.extension;
                  $config['max_size']=MAXSIZE; //max size constant
                  $config['max_width']=MAXWIDTH; //max width constant
                  $config['max_height']=CMPMAXHEIGHT; //max height constant
                  $config['allowed_types']='jpg|png';

                  if(!is_dir($config['upload_path'])){
                      mkdir($config['upload_path'], 0775);
                  }
                  $this->upload->initialize($config);
                  $imageuploaderres[]=$this->upload->do_upload('mediaelement');
              }
          }

Upvotes: 0

Goose
Goose

Reputation: 4821

You can upload multiple files with CodeIgniter in a single request. You just need to do do_upload() for each file. Here's one implementation. You can work it into a loop if needed.

    // Image upload Config
    $config['upload_path']   = './uploads/';
    $config['allowed_types'] = 'gif|jpg|jpeg|png';
    $config['max_size']      = '1000000';
    $config['encrypt_name']  = TRUE;
    $this->load->library('upload', $config);

    // Upload Files
    if ( $_FILES['first_file']['name'] && $this->upload->do_upload('first_file') ) {
        $first_file = $this->upload->data();
    }
    if ( $_FILES['second_file']['name'] && $this->upload->do_upload('second_file') ) {
        $second_file= $this->upload->data();
    }

Upvotes: 0

Mitul
Mitul

Reputation: 3427

Please try bellow

for($i=0; $i<$cpt; $i++)
{           
    $_FILES['files'] =  $files[$i];
    $this->upload->initialize($this->set_upload_options());
    if (!$this->upload->do_upload('files'))
    {  
        $error =['error' => $this->upload->display_errors()];
        $this->load->view('admin/addGallery', $error);
    }
}

Upvotes: 0

Tariq Husain
Tariq Husain

Reputation: 566

you did not pass file name to upload function.Try this.

if (!$this->upload->do_upload($_FILES['files']['name']))
        {  
            $error =['error' => $this->upload->display_errors()];
            $this->load->view('admin/addGallery', $error);
}

Upvotes: 2

Sanoob
Sanoob

Reputation: 2474

By default codeIgniter doesn't support multi-file upload. So you can use this library CodeIgniter Multiple Upload Library

Upvotes: 3

Ricky
Ricky

Reputation: 568

try this:

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

and make a function set_upload_options()

$config = array();
         $config['upload_path'] = './upload/'; //give the path to upload the image in folder
         $config['allowed_types'] = 'gif|jpg|png';
          $config['max_size'] = '0';
         $config['overwrite'] = FALSE;
  return $config;

For more Upload multiple files using codeigniter try this http://w3code.in/2015/09/upload-file-using-codeigniter/

Upvotes: 0

kjames
kjames

Reputation: 656

I think you need to change this line:

if (!$this->upload->do_upload('files[]'))

to

if (!$this->upload->do_upload('files'))

Upvotes: 2

Related Questions