0

I want to import excel using php. I have

 public function importexcel()
    {

        $this->img = 'poc.xlsx';
$exceldata = array();
 require 'Classes/PHPExcel/IOFactory.php';
try
{
    $inputfiletype = PHPExcel_IOFactory::identify( $this->img);
    $objReader = PHPExcel_IOFactory::createReader($inputfiletype);
    $objPHPExcel = $objReader->load( $this->img);
}

catch(Exception $e)
{
    die('Error loading file "'.pathinfo( $this->img,PATHINFO_BASENAME).'": '.$e->getMessage());
}

POC.xlsx

enter image description here

But when I load the page I got an error like

Error loading file "poc.xlsx": Could not open poc.xlsx for reading! File does not exist.

Please help me. Any help would be appreciated.

3
  • 1
    It looks like your path to file is not correct. Please check your file path. Commented Mar 15, 2018 at 7:24
  • 1
    'controllers/poc.xlsx' Commented Mar 15, 2018 at 7:27
  • @LawrenceCherone thanks Commented Mar 15, 2018 at 7:50

1 Answer 1

1

You should always use absolute paths for files. If the file is in the same directory as the controller, the line with the path should look as follows:

$this->img = __DIR__ . '/poc.xlsx';

__DIR__ is a magic constant, whose value always equals the absolute path to the directory of the current file.

Notice that you're using a require statement like this:

require 'Classes/PHPExcel/IOFactory.php';

Since you obviously don't have a fatal error informing about an inexistent file, it indicates that the current working directory is not controllers, but one above it. So, you could also write

$this->img = '../poc.xlsx';` 

and this would probably work too. But again, it'll be easier to avoid all errors with missing files when you use absolute paths.

Sign up to request clarification or add additional context in comments.

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.