1

I am working on project where I need to read an excel file sent in a form and then extract data from it. I am trying to use Phpspreadsheet library for the purpose but Phpspreadsheet library does not seems to recognize the file and throws an error: The file does not exist.

$product_serial = $this->input->post('product_serial');

    $excelFilePath = base_url().'userfiles/product/excelfiles/'.$product_serial;



    try {
        $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReaderForFile($excelFilePath);
        $spreadSheet = $reader->load($excelFilePath);

        $dataAsAssocArray = $spreadSheet->getActiveSheet()->toArray();



    }
    catch (\Exception $exception){

        echo $exception->getMessage();

    }

Error: File "http://localhost:8000/userfiles/product/excelfiles/452260670768Untitledspreadsheet.xlsx" does not exist

I have checked the file at above location the file does exist there!

Nauman Bashir
  • 101
  • 1
  • 7
  • As stated on Cesare's answer, there's a difference between URLs and Paths. When accessing internal resources you should always use paths. Even though they may seem interchangeable, they are not – Javier Larroulet Sep 09 '20 at 15:32

1 Answers1

1

Codeigniter read the file from filesystem so you must use as the PATH instead of URL.

Actually you are using the URL:

base_url().'userfiles/product/excelfiles/'.$product_serial;

You need build your path from FCPATH

FCPATH.'userfiles/product/excelfiles/'.$product_serial;

For more information on Code igniter constants please follow these links

https://codeigniter.com/user_guide/general/common_functions.html?#global-constants

Codeigniter - dynamically getting relative/absolute path outside of application folder

cesare
  • 2,098
  • 19
  • 29