What are the potential pitfalls of using a counter with reload lock in PHP?

One potential pitfall of using a counter with reload lock in PHP is that it can lead to synchronization issues if multiple users are accessing the counter simultaneously. To solve this issue, you can use a file lock to ensure that only one user can access and update the counter at a time.

$lockFile = fopen("counter.lock", "w");
if (flock($lockFile, LOCK_EX)) {
    $counter = file_get_contents("counter.txt");
    $counter++;
    file_put_contents("counter.txt", $counter);
    flock($lockFile, LOCK_UN);
} else {
    echo "Could not acquire lock on counter file.";
}
fclose($lockFile);