PHP Problem When Opening Same File?

  • From: "Bryan Garaventa" <bgaraventa11@xxxxxxxxxxxxxx>
  • To: <programmingblind@xxxxxxxxxxxxx>
  • Date: Wed, 2 Jan 2008 20:20:33 -0800

Is it possible to query a file on the server to see if it's already opened by 
another script?

My problem is this...

I have a variable that is dynamically updated by a script, which increments a 
counter each time the page loads. 

However, if two people simultaniously trigger the script to run at virtually 
the same moment, which has just recently happened, the first instance of the 
script takes the variable, opens the file and then writes the new value into 
the file, then closes it. At the same time, the second instance of the script 
is trying to access the same file, and finding no file (since it's already 
opened by the other instance), the counter defaults to zero, and that is then 
written in the new file instead of the incremented value.

All of which totally screws up the counter.

So, is there a way that I can check if the file is already open before having 
the script try to open the file and write new data to it?

Thanks,

Bryan


Other related posts: