0

I'm trying to scan hundreds of folders each containing an excel file.

Here's the simplified workflow

  • scan folders in loop
  • get first folder name and run it trough some regex (to get some info from the name)
  • open the folder and open the excel file inside
  • scan excel file for info's (with PHPExcel)
  • save everything to Database

Ok so all this is working just fine, except when i run into an excel file which is corrupted.

The problem then is that the PHPEXCEL library cant read it and it throughs back a "Notice: Undefined offset:" and this breaks my foreach loop.

So here is what I'm looking for: a way to somehow skip or something the erros and notices and continue with the next folder and file.

Is there a way? Thank's for any help.

Ok so here is the foreach loop:

foreach ($inputFileName as $key => $fileName) {
$objReader = PHPExcel_IOFactory::load($fileName);

$activeSheet = $objReader->getActiveSheet()->toArray(null,false,true,false);

echo "<pre>";
    echo $key;
echo "</pre>";
}
10
  • 1
    This sounds like a bug in the phpexcel library. Commented Oct 10, 2013 at 8:23
  • Maybe a try/catch can handle the error. Commented Oct 10, 2013 at 8:24
  • @Barmar You can't catch notice, only exceptions. Commented Oct 10, 2013 at 8:24
  • If these are just notices, just lower the error reporting setting. Commented Oct 10, 2013 at 8:25
  • 1
    @cbuckley fallowing your idea i went to the phpexcel library and solved the problem there. So thank you. Commented Oct 10, 2013 at 9:15

2 Answers 2

1

Just a guess:

foreach ($inputFileName as $key => $fileName) {
    $objReader = PHPExcel_IOFactory::load($fileName);

    if ($objReader) {
        $activeSheet = $objReader->getActiveSheet()->toArray(null,false,true,false);

        echo "<pre>";
            echo $key;
        echo "</pre>";
    }
}

It's pretty common for functions to return false when they fail.

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

1 Comment

unfortunately that didn't work, but it kind of gave me a direction, i didn't wanted to do this but now i think my only solution is to hack the PHPEXCEL library, to give some result if it cant open an excel
0

Maybe you can use the function error_reporting at the beginning of your script to skip notices :

error_reporting(E_ERROR | E_WARNING | E_PARSE);

And set it back to your original value once you are done with your treatment :

error_reporting(-1); //Report all errors

You can find more info here : http://php.net/manual/fr/function.error-reporting.php

2 Comments

like i told @Barmar i tried that but the loop still stops, without notice
@Mr.Sam just do error_reporing(0) at the top of the script, then

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.