One simple thing you can try is to use flock to get an exclusive file lock and if it doesn’t work, you will know that the file is being used:
<?php $fp = fopen('c:/your_file.xlsx', 'r+'); if(!flock($fp, LOCK_EX)) { echo 'File is being used...'; exit(-1); } else { fclose($fp);
An alternative would be to check for the existence of a lock file. Normally excel creates when a file is used:
<?php $file = 'c:/testfile.xlsx'; $lock = 'c:/~$testfile.xlsx'; if (file_exists($lock)) { echo "Excel $file is locked."; } else { echo "Excel $file is free."; }
The hidden file usually has a name with the prefix ~$ , as for old excel files, which, it seems to me, in 2003 and older, lock files are stored in the temp folder with a random name like ~DF7B32A4D388B5854C.TMP , so it would be pretty hard to find from .
source share