6

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.

3 Answers 3

4

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';
Sign up to request clarification or add additional context in comments.

4 Comments

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)
updated my answer for not hardcoding the absolute path, and tell me if it works
yes it is already worked when tried ./myproject/uploads/Data_Report.xls without BASE_URL()
use the codeigniter paths so if you move the project directories it will still work. Please accept the answer if it helped you :)
1

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.

Comments

0

first, check the upload_max_filesize in php.ini file. if your excel size < to upload_max_filesize size this error will happend.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.