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 .
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';