Putra L Zendrato
Putra L Zendrato

Reputation: 332

Codeigniter Upload multiple files different path to database

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));
}

Upvotes: 3

Views: 7852

Answers (2)

al. mamun
al. mamun

Reputation: 1

$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);
            }
            }

Upvotes: 0

Dino Babu
Dino Babu

Reputation: 5809

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

Upvotes: 9

Related Questions