phpphpexcelphpexcelreader

Return message if reading an empty file


I am using PHP Excel library. I want to show a error message to the user when user uploads an empty file for reading. I google'd for it but couldn't get relevant answer.

When a new excel file is created it has a default filesize of 9KB in windows. I cannot use filesize() to check for emptyness as suggested by some of the post here.

Is there any way using PHP Excel to check if the file is empty and return error message??


Solution

  • Well, there is no direct method to tell that file is empty. You can tell that file is empty when its sheet contains no data.

    You can test than in multiple ways, for example:

    1) $objPHPExcel->getActiveSheet()->toArray() It will return sheet data as an array, if count = 0 - your sheet is empty.

    2) $objPHPExcel->setActiveSheetIndex(0)->getHighestColumn();

    and $objPHPExcel->setActiveSheetIndex(0)->getHighestRow();

    If higest row = 1, and column = A - your sheet is empty.