PHP - Codeigniter: throw exception "PHPExcel_Reader_Exception" with the message "Could not be opened for reading, File does not exist

My current CI project is now processing files. I want to download xls | xlsx to the server, and then import the excel file data into the database table.

as the first part, the downloaded file was successful, and I upload files to the uploads folder at the same level of application, system, assets . Now I want to download this file and import this content into the database. I am using PHPExcel to perform 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 run it, the Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open http://localhost/myproject/uploads/Data_Report.xls for reading! File does not exist.' error Uncaught exception 'PHPExcel_Reader_Exception' with message 'Could not open http://localhost/myproject/uploads/Data_Report.xls for reading! File does not exist.' 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 this Data_Report.xls file inside htdocs , it works successfully.

The problem is that im uses BASE_URL (). 'uploads / Data_Report.xls'. but the file physically exists, and I checked by inserting localhost / myproject / uploads / Data_Report.xls into the url and it downloaded successfully.

Any help would be greatly appreciated.

+6
source share
2 answers

I'm not sure if this is the case, but you should try adding the absolute path to the file system, not the path to the URL.

as:

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

but not

 yourUrl.com/uploads/Data_Report.xls 

how to get the absolute path (without hardcoding!) using codeigniter you can use:

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

so I don’t remember the codeigniter structure, but I got it from google, so do something like this:

 APPPATH.'public/uploads/what_ever.xls'; 
+4
source

Is the xls file copied to the upload folder when the form is submitted?

It will be something like below:

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

by default, the file is uploaded to the temp directory (in your case, it is most likely htdocs), and it must be transferred to the target path before Excel opens through PHP Excel lib.

+1
source

Source: https://habr.com/ru/post/980350/


All Articles