PHP - Codeigniter : Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open for reading, File does not exist

11,411

Solution 1

I'm not sure if that is the case, but you should try to add the absolute path in file system and not the path of the url.

like:

/var/www/YourProject/public/uploads/Data_Report.xls

and not

yourUrl.com/uploads/Data_Report.xls

how to get the absolute path (no hardcoding!) with codeigniter you can use:

FCPATH   -> '/'
BASEPATH -> '/system/'
APPPATH  -> '/application/'

so I dont remember the structure of codeigniter but I got this from google, so do something like this:

APPPATH.'public/uploads/what_ever.xls';

Solution 2

Are you doing copy of xls file to your "upload" directory on form submission?

It will be something like below:

if (!move_uploaded_file( $_FILES['file']['tmp_name'], $target_path ) ) { throw new RuntimeException('Failed to move uploaded file.'); }

by default File is uploaded in temp directory (in your case it seem to be htdocs) and it need to be moved to target path of choice before performing Excel open through PHP Excel lib.

Share:
11,411
Shifana Mubi
Author by

Shifana Mubi

Interested to develop new programs and codes as Challenge.Focusing on core programs that i considered as i can't. Interested Areas: PHP , Codeigniter , Javascript , Jquery , Bootstrap and many more... Be a Programmer...

Updated on June 23, 2022

Comments

  • Shifana Mubi
    Shifana Mubi almost 2 years

    My current CI project is now handling with files. i want to upload a xls| xlsx files to the server and then import excel file data into the Database table.

    as the first part , the file uploaded was successful, and i upload the files to the uploads folder in the same level of application , system, assets . now i want to load this file and import those contents to DB. I'm using PHPExcel to do this operation

    Here is my Controller

    $this->load->library('phpexcel');
    $this->load->library('PHPExcel/iofactory');
    
    $objPHPExcel = new PHPExcel();
    
    $objReader= IOFactory::createReader('Excel2007');
    $objReader->setReadDataOnly(true);
    
    $objPHPExcel=$objReader->load(BASE_URL().'uploads/Data_Report.xls');
    
    $objWorksheet=$objPHPExcel->setActiveSheetIndex(0);
    
    $this->load->model('datas_model');
    
    for($i=2;$i<=77;$i++) {
    
         $client_name= $objWorksheet->getCellByColumnAndRow(0,$i)->getValue();
         $client_address= $objWorksheet->getCellByColumnAndRow(1,$i)->getValue();
         $data_inp=array('name'=>$client_name, 'address'=>$client_address);
         $this->datas_model->add_data($data_inp);
       }
    

    and here is my View

    <?php echo form_open_multipart('../settings_controller/upload_data/do_upload');?>
    
       <div class="custom-file-upload">
            <input type="file" id="file" name="userfile" multiple/>
       </div>
       <div class="button-container-2">
            <button class="btn btn-primary" id="updown-btn" type="submit" style="height:45px;">Upload </button>
       </div>
    
    <?php echo "</form>"?>
    

    when I'm running it shows me an error Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open http://localhost/myproject/uploads/Data_Report.xls for reading! File does not exist.'

    when i put that Data_Report.xls file inside htdocs , it works succesfully .

    The problem is when im using BASE_URL().'uploads/Data_Report.xls'. but the file is physically there and i verified by pasting localhost/myproject/uploads/Data_Report.xls on url and it downloaded successfully.

    Any help would be greatly appreciated .

  • Shifana Mubi
    Shifana Mubi over 9 years
    now i get the solution when provide the relative path by removing base_url.but is this correct method to solve this problem .? becouse it do not uses the base_url .? so is there any problem in future (hosting)
  • Tzook Bar Noy
    Tzook Bar Noy over 9 years
    updated my answer for not hardcoding the absolute path, and tell me if it works
  • Shifana Mubi
    Shifana Mubi over 9 years
    yes it is already worked when tried ./myproject/uploads/Data_Report.xls without BASE_URL()
  • Tzook Bar Noy
    Tzook Bar Noy over 9 years
    use the codeigniter paths so if you move the project directories it will still work. Please accept the answer if it helped you :)