Reputation: 197
My current CI project is now handling files. I want to upload the xls| xlsx files to the server and then import Excel data into the Database table.
As the first part, the uploaded file was successfully, and I uploaded 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 the 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 using BASE_URL().'uploads/Data_Report.xls'. But the file is physically there. I verified by pasting localhost/myproject/uploads/Data_Report.xls on the URL, and it downloaded successfully.
Any help would be greatly appreciated.
Upvotes: 6
Views: 12055
Reputation: 547
Are you copying the 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, the file is uploaded in the temp directory (in your case it seems to be htdocs
) and needs to be moved to the target path of choice before performing Excel open through PHP Excel library.
Upvotes: 1
Reputation: 61
first, check the upload_max_filesize in php.ini file. if your excel size < to upload_max_filesize size this error will happend.
Upvotes: 0
Reputation: 11677
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';
Upvotes: 4