tags:

views:

69

answers:

3

How do I delay a PHP script that writes to a text file and then reads from the same file long enough to make sure changes have been written before I attempt the read?

A: 

Just flush the file's write buffer and then you should be good to go:

http://us.php.net/manual/en/function.fflush.php

Amber
Ah - that's perfect! Thanks :-)
safetycopy
If you need to do that then there's something **very** broken in your operating system or PHP. Or you're opening a second file handle on the file (without closing the first) instead of using the original handle or a duplicate. Which means your code is broken.This is *NOT* how to fix the issue.
symcbean
OK, if that's not the right way, what would you recommend?
safetycopy
symcbean, it's possible in scenarios where one is using the `w+` or `a+` options.
Amber
A: 

@symcbean is correct. You need to first close the handle you are working on using fclose. Then open up the read connection. The way PHP works is that it won't move to the next line until the previous line has completed operations. So if you are concerned with the read running after the write, then make sure that is where it is in the code. This is different from java and javaScript where triggers cause pieces of the code to run.

Joseph
Thanks for the detailed explanation! :-)
safetycopy
A: 

You should learn to use flock (lock a file for writing, reading).

Brendon
Will look into it - thanks :-)
safetycopy