Reputation: 542
I have a question. I have a script that's fired by a GET trigger. I'm trying to log the executions of the script using fwrite and append, but what I have happening is that if an instance is called while another instane is still writing the file, it will interrupt the fwrite and insert its own data. Is there any way I can queue the script to wait in case the file is being written to? But I don't want to lose any of the log data.
Thanks in advance!
Upvotes: 0
Views: 227
Reputation: 48357
Using flock is a really bad idea - you then get into the realms of working out how long a file should locked for, articially suspending execution of your script - all kinds of nastiness. PHP does not have the kind of file locking semantics needed for concurrent file access.
Better solutions are:
Upvotes: 1
Reputation: 10880
You should look into flock()
probably. It might also be a good idea to have a create a separate file for each execution and then merge them at the end by locking the bigger log file or you might even consider sending everything to the database if it is feasible.
Upvotes: 1