Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 .

like image 379
Shifana Mubi Avatar asked Dec 31 '14 07:12

Shifana Mubi


2 Answers

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';
like image 130
Tzook Bar Noy Avatar answered Nov 20 '22 00:11

Tzook Bar Noy


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.

like image 1
adang Avatar answered Nov 19 '22 23:11

adang