Skip to content

Instantly share code, notes, and snippets.

@miholeus
Created December 27, 2017 07:55
Show Gist options
  • Save miholeus/fb1cf036ffe983e732a5fc375b377a4f to your computer and use it in GitHub Desktop.
Save miholeus/fb1cf036ffe983e732a5fc375b377a4f to your computer and use it in GitHub Desktop.
file lock example
$file = fopen("counter.txt", "r+");
if (flock($file, LOCK_EX)) {
$data = fread($file, 64);
$data = trim($data);
if (empty($data)) {
$data = 0;
}
$data++;
ftruncate($file, 0);
fwrite($file, $data);
fflush($file);
flock($file, LOCK_UN);
} else {
throw new \RuntimeException("File is locked");
}
fclose($file);%
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment