3

I have a problem with my multiple upload with codeigniter,

  1. using image
  2. another using pdf

When I uploaded the file uploaded twice and how to call different path to uploaded to database. this my code

Controller

public function upload(){

    $catalog='catalog';
    $userfile='userfile';

    //for cover
    $config['upload_path'] = './file/book/'; //Use relative or absolute path
    $config['allowed_types'] = 'gif|jpg|png|'; 
    $config['max_size'] = '100000';
    $config['max_width'] = '1024';
    $config['max_height'] = '768';
    $this->load->library('upload', $config);
    $this->upload->initialize($config); 

    //$c=$this->upload->do_upload($userfile);

    //for catalog
    $config['upload_path'] = './file/book/pdf/'; //Use relative or absolute path
    $config['allowed_types'] = 'pdf'; 
    $config['max_size'] = '1000000';
    $this->load->library('upload', $config);
    $this->upload->initialize($config);

    //$cat=$this->upload->do_upload($catalog);


    if(!$this->upload->do_upload($catalog) &&         $this->upload->do_upload($userfile)){


        $error = array('error' => $this->upload->display_errors());
        $this->load->view('uploadds', $error);

    }else{ 



        $this->load->model("book_model"); 
        $this->book_model->addnewbook();
        redirect('book/book');      
    }

}

This model

function addnewbook(){
     $fcat=array('upload_data' => $this->upload->data($userfile));
     $fcatalog=array('upload_dataa' => $this->upload->data($catalog));
}
1
  • A quick google search gave me this. pastebin.com/g9J6RxW1. Haven't tried yet. Will try and let you know. Commented Feb 27, 2014 at 8:49

2 Answers 2

9

You need to handle multiple uploads independently. For this, you have to create separate custom objects for both uploads while loading the upload library. (See the code comments)

  public function upload() {

    // Cover upload
    $config = array();
    $config['upload_path'] = './file/book/';
    $config['allowed_types'] = 'gif|jpg|png|';
    $config['max_size'] = '100000';
    $config['max_width'] = '1024';
    $config['max_height'] = '768';
    $this->load->library('upload', $config, 'coverupload'); // Create custom object for cover upload
    $this->coverupload->initialize($config);
    $upload_cover = $this->coverupload->do_upload('cover');

    // Catalog upload
    $config = array();
    $config['upload_path'] = './file/book/pdf/';
    $config['allowed_types'] = 'pdf';
    $config['max_size'] = '1000000';
    $this->load->library('upload', $config, 'catalogupload');  // Create custom object for catalog upload
    $this->catalogupload->initialize($config);
    $upload_catalog = $this->catalogupload->do_upload('catalog');

    // Check uploads success
    if ($upload_cover && $upload_catalog) {

      // Both Upload Success

      // Data of your cover file
      $cover_data = $this->coverupload->data();
      print_r($cover_data);

      // Data of your catalog file
      $catlog_data = $this->catalogupload->data();          
      print_r($catlog_data);
    } else {

      // Error Occured in one of the uploads

      echo 'Cover upload Error : ' . $this->coverupload->display_errors() . '<br/>';
      echo 'Catlog upload Error : ' . $this->catalogupload->display_errors() . '<br/>';
    }
  }

Use the data on $cover_data['full_path'] and $catlog_data['full_path'] to update your database

Sign up to request clarification or add additional context in comments.

2 Comments

You don't need to load the library multiple times, you can just call initialize on the same library and it will switch the configs out...
Exactly, perfect. I couldnt find this even in codeigniter docs. Thanks alot!
0
$config['upload_path'] = 'frontend_assets/images/hospital';
$config['allowed_types'] = 'gif|jpg|png|jpeg|JPEG||JPG|PNG';
$this->load->library('upload', $config);

if($_FILES['logo']['name']){
$config['file_name'] = time() . $_FILES["logo"]['name'];

  if (!$this->upload->do_upload('logo')) {

    $this->upload->display_errors();

            } else {

                $upload = $this->upload->data();

                $insert_data['logo'] = $config['upload_path'] . '/' . $upload['file_name'];

            }
            }


if($_FILES['bulding_photo']['name']){
                 $config['file_name'] = time() . $_FILES["bulding_photo"]['name'];

            if (!$this->upload->do_upload('bulding_photo')) {

                $this->upload->display_errors());

            } else {

                $upload = $this->upload->data();

                $insert_data['bulding_photo'] = $config['upload_path'] . '/' . $upload['file_name'];
                $this->image_size_fix($insert_data['bulding_photo'], $width = 200, $height = 200);
            }
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.