Search code examples
phpexcelphpspreadsheet

PhpSpreadsheet - Check if file is open


I need to check if the excel file is currently open using PHPSpreadsheet.

I thought of a solution by handling this line $writer->save(file_name) into if else condition but still doesn't work.

if($writer->save(file_name){
    //file is open
}else{
    //file is not open
}

What else can I do to solve my problem?


Solution

  • I need to check if the excel file is currently open using PHPSpreadsheet.

    Checking if a file is open isn't really useful, since the open/closed state could change at any time, or the failure could be caused by other filesystem problems.

    What you need to do is handle the failure during file operations as:

    try {
         $writer->save(file_name);
        }
    
    catch (Exception $e) {
        echo 'Unable to save file. Please close any other applications(s) that are using it: [",  $e->getMessage(), "]\n";
    }