Created
December 27, 2017 07:55
-
-
Save miholeus/fb1cf036ffe983e732a5fc375b377a4f to your computer and use it in GitHub Desktop.
file lock example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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