Check valid Excel format

Topics: Developer Forum
Feb 25, 2009 at 10:02 AM
Hello people,

I was wondering if it is possible to check the uploaded excel file for its correctness.
If i rename for example photo.jpg file into fakeexcel.xls, upload it to my php page, i get an error Fatal error: Uncaught exception 'Exception' with message '
This is pretty logically, but is there a way to check the file first and give a decent error message?

Many thanks in advance,
Jasper
Developer
Feb 25, 2009 at 10:14 AM
A try-catch block might suffice for catching exceptions. This is like branching. Example:

try {
    $excel = $reader->load('example.xls');

} catch (Exception $e) {
    echo $e->getMessage();
    exit;
}

At least the case you are describing will usually be caught, but it is not bulletproof.
Feb 25, 2009 at 10:33 AM
Ok, nice! Many thanks again!!