PHP Check the file is opend before rename -
i have cleanup script, move xls files 1 place another. file moving process, have used rename function. script working fine. when xls file open, when try move xls, getting error can not rename sample.xls. add functionality like, check xls open before initiate rename function.
i believe function call flock
applicable txt file alone.
how check xls file opened before call rename function.
one simple thing try use flock
acquire exclusive lock on file , if fails know file being used:
<?php $fp = fopen('c:/your_file.xlsx', 'r+'); if(!flock($fp, lock_ex)) { echo 'file being used...'; exit(-1); } else { fclose($fp); // rename(...); }
an alternative check existence of locking file excel creates when file being used:
<?php $file = 'c:/testfile.xlsx'; $lock = 'c:/~$testfile.xlsx'; if (file_exists($lock)) { echo "excel $file locked."; } else { echo "excel $file free."; }
the hidden file name prefix ~$
old excel files believe 2003 , older lock files saved on temp folder random name ~df7b32a4d388b5854c.tmp
pretty hard find out.
Comments
Post a Comment