I want to prevent a PHP script from running at the same time. Which I achieved by checking if a 'lock.txt' file exists. This however takes multiple seconds, even though it should be very fast.
<?php
if (!file_exists('lock.txt')) {
$lock = fopen('lock.txt','w');
////////////////////////////////////////////
......script that takes 30-160 seconds......
////////////////////////////////////////////
$jsonArray = array(utf8_encode('script')=>utf8_encode('finished'));
fclose($lock);
unlink('lock.txt');
} else {
$jsonArray = array(utf8_encode('script')=>utf8_encode('locked'));
}
echo json_encode($jsonArray);
?>
Why does it take that long? It should just fail the initial if statement and quickly echo json_encode
.
am I doing something wrong?