views:

77

answers:

3

Hey all,

I have a question about how PHP handles filesystem operations. I'm running this code that depends on a file being created before it gets used, and it feels like when I run the code it becomes a race condition - sometimes the it works, the file is created and php code uses it, sometimes it fails.

So I was wondering how php handles filesystem operations, does it send it off in the background or does it wait till the operation complete?

+2  A: 

PHP should wait until the process is completed. But not knowing how you are implementing the operations it is hard to say. If you can post an example code that you are using that would be helpful so we can help you figure out why it is not working properly.

Brad F Jacobs
+2  A: 

Yes, unless you open a file handle and then set it to non-blocking mode: stream_set_blocking()

ircmaxell
This is actually not true. If you open and simply (f)write to it, the data is not written to disk. If fwrite uses the C function fwrite, with a high chance the data has not even reached the operating system. As @Matthew Flaschen pointed out, the data is only flushed to disk during fclose and fflush.
dmeister
+3  A: 

file_put_contents is equivalent to fopen, fwrite, fclose. fclose should ensure the file is fully flushed to disk.

Matthew Flaschen
Actually, the only way to know for sure that the file is fully flushed to disk is to turn off write-through caching on the drives. Otherwise, all you can tell is that the filesystem issued the write commands, not that the write was actually committed successfully to disk...
ircmaxell