EDIT #2:
Try this with flock (tested)
If file is not locked, it will throw an Exception (see added line) if
(...
I borrowed the Exception snippet from this accepted answer.
<?php
$filename = "numbers.txt";
$filename = fopen($filename, 'a') or die("can't open file");
if (!flock($filename, LOCK_EX)) {
throw new Exception(sprintf('Unable to obtain lock on file: %s', $filename));
}
file_put_contents('numbers.txt', ((int)file_get_contents('numbers.txt'))+1);
// To show the contents of the file, you
// include("numbers.txt");
fflush($filename); // flush output before releasing the lock
flock($filename, LOCK_UN); // release the lock
fclose($filename);
echo file_get_contents('numbers.txt');
?>