coltsith Posted February 21, 2008 Share Posted February 21, 2008 Hi there, I'm currently trying to read a list of values in a text file, and then at the end, go back to the very beginning and write a new list of values. I've tried Rewind(file) but that only seems to rewind the reader back to the beginning of the current line. How do I make it rewind all the way back to the beginning of the entire file? I need to do this because I need this operation synchronized and am using fLock(). And if I close the file and open it again to write, there could potentially be problems in between the closing of the first read, and the opening of the second: flock($a) fopen($a, read) fclose($a) // < ---- Not synchronized; Someone else calling this script could potentially open the file // here. flock($a) fopen($a, write) fclose($a) What I'd like to be able to do is: flock($a) fopen($a, read+write) // read down list fRewindBackToBeginningOfFile($a) // write new list fclose($a) Thanks for any help you can give! Quote Link to comment https://forums.phpfreaks.com/topic/92343-while-reading-text-file-rewind-back-to-beginning/ Share on other sites More sharing options...
Daniel0 Posted February 21, 2008 Share Posted February 21, 2008 fseek($a, 0); Quote Link to comment https://forums.phpfreaks.com/topic/92343-while-reading-text-file-rewind-back-to-beginning/#findComment-473124 Share on other sites More sharing options...
rhodesa Posted February 21, 2008 Share Posted February 21, 2008 Flock is used on a file handle, not a filename. The proper way to use flock is with a seperate lock file...so something like: <?php $file = 'myfile.txt'; $FL = f_open("{$file}.lck","a+"); //Open Lock File f_lock($FL,LOCK_EX); //Start lock $FH = f_open($file,'w'); //Open file // do operations here f_close($FH); //Close File f_close($FL); //Close Lock ?> NOTE: To post the code, I had to add underscores in the function names. Make sure you remove those. Quote Link to comment https://forums.phpfreaks.com/topic/92343-while-reading-text-file-rewind-back-to-beginning/#findComment-473131 Share on other sites More sharing options...
coltsith Posted February 21, 2008 Author Share Posted February 21, 2008 Okay thanks guys, I'll try using the locks as you specified and fseek(). Quote Link to comment https://forums.phpfreaks.com/topic/92343-while-reading-text-file-rewind-back-to-beginning/#findComment-473188 Share on other sites More sharing options...
coltsith Posted February 22, 2008 Author Share Posted February 22, 2008 quick question, probably silly, but does the separate lock file need to exist? So do I have to really have the file "file.txt.lck"? Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/92343-while-reading-text-file-rewind-back-to-beginning/#findComment-473399 Share on other sites More sharing options...
rhodesa Posted February 22, 2008 Share Posted February 22, 2008 yes...it's just an empty file Since you can't lock a file until after you get a File Handle open, it needs to be a separate file that gets open and locked before the actual file is worked on. Check out the different kind of locks you can make. If one piece of code is only reading the file, do a shared lock (multiple instance can read at the same time. Then use the exclusive lock with the piece of code that writes, to make sure no one else is accessing the file while you are changing it. Quote Link to comment https://forums.phpfreaks.com/topic/92343-while-reading-text-file-rewind-back-to-beginning/#findComment-473646 Share on other sites More sharing options...
coltsith Posted February 22, 2008 Author Share Posted February 22, 2008 Okay, thanks! I've got the script written and basically working. One last question: I'm using this script to write a table, and I want another script to read from it and put the values into an Html Table. Do I have to use a lock for the reading script? Can I just read from the file with no locks? I'm not worried about the user viewing information that's out of date and needs to refresh, I'm only worried about it possibly screwing up the writing process if the write script is being called. Quote Link to comment https://forums.phpfreaks.com/topic/92343-while-reading-text-file-rewind-back-to-beginning/#findComment-473863 Share on other sites More sharing options...
rhodesa Posted February 22, 2008 Share Posted February 22, 2008 Use a shared lock(LOCK_SH) on the read script and an exclusive lock(LOCK_EX) on the write script. That way scripts will be able to simultaneously read with no problem, until the file is opened for writing. Then the read scripts will wait while the write script is working. Quote Link to comment https://forums.phpfreaks.com/topic/92343-while-reading-text-file-rewind-back-to-beginning/#findComment-473899 Share on other sites More sharing options...
coltsith Posted February 23, 2008 Author Share Posted February 23, 2008 What if it's okay for the read scripts to read the table while the write script is working? Doesn't that mean that they'll read from the table before it got written to? Or is there a chance that the table will be screwed up in the write process? Quote Link to comment https://forums.phpfreaks.com/topic/92343-while-reading-text-file-rewind-back-to-beginning/#findComment-474266 Share on other sites More sharing options...
rhodesa Posted February 25, 2008 Share Posted February 25, 2008 Yeah, there is a chance they will read the data mid write. The write should take a matter of milliseconds, so you shouldn't even notice it in the read script. And the change of something reading it while you are writing are very small. But if you are looking to be safe, the above way is the best approach (in my opinion). Quote Link to comment https://forums.phpfreaks.com/topic/92343-while-reading-text-file-rewind-back-to-beginning/#findComment-475536 Share on other sites More sharing options...
coltsith Posted February 25, 2008 Author Share Posted February 25, 2008 Okay, thanks a lot!! Quote Link to comment https://forums.phpfreaks.com/topic/92343-while-reading-text-file-rewind-back-to-beginning/#findComment-475652 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.