0

I have a script that runs with each visitor. Each visit writes some information to a file. I first check if the file exists using file_exists, and it if does I write to it using file_put_contents using the LOCK_EX flag. For example:-

$file = "myfile.txt";
if ( file_Exists($file) ) {
    file_put_contents($file, "some content", LOCK_EX);
} else {
    echo "file does not exist";
}

However, on rare occasions, during high traffic, the file_exist check fails even though the file exists. I'm assuming the file lock is preventing file_exists from checking the file, but I cant find any information that confirms this.

Can anybody corroborate my assumption, or is there another possible reason for the file_exists function to throw false?

Thanks

  • Unless the filename changes for each session, you're going to encounter collisions under high traffic. One person creates the file in between when the other one checks for it and tries to create it themselves. – Alex Howansky Jan 30 '18 at 20:54
  • File status check is cached. So file lock should not affect the return of file_exists if the file do exist. – ild flue Jan 30 '18 at 20:58
  • @ildflue. I forgot that this function is cached. Its a good point. So, if its not the lock thats causing the function to return false on occasion, any idea what else might cause this? – Michae Pavlos Michael Jan 30 '18 at 21:14

0 Answers0