Should I use flock?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nitinjavakid
Forum Newbie
Posts: 3
Joined: Tue Jan 09, 2007 11:45 am

Should I use flock?

Post by nitinjavakid »

I am developing a messaging system for which I dont have a database. :(
Everything is complete(well it seems so). But, I was wondering what if in the same file which is being read and written to by many users is being accessed but two users to write into. Will it conflict. See for editing any message a this is what I am doing.

1) creating a temp file
2) copying the contents of original file and also the changed message
3) deleting the original file
4) renaming the temp file to original file.

Now here is my doubt. What if at the very instant at which I delete the original file another user asks for it before renaming?? What if two users read the same file and then edited messages and put renamed their respective temp files? Should I take into consideration all these problems? If so how can I solve them. Please specify an algo similar to the one mentioned above.

Regards
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Your title sounded like: Should I use Flock? .... sure, I guess ;)

After reading (and understanding) your question: you should use a database.

Failing that, you're in for a bumpy ride! Maybe your script could keep trying to update the file until it succeeds?
Post Reply