Mike521 Posted May 28, 2010 Share Posted May 28, 2010 I have a text file that is written to by two scripts: 1. writes order data whenever a new order comes in 2. must read the order data and then erase the file, twice a day This way every time script 2 runs, it has only the new orders, none that have already been read. Each of the scripts does an flock to make sure the other one doesn't access it while it's making changes. Problem is, after script 2 reads all the orders, I want to erase the text file. If I close the file and then reopen it in "w" mode, there'll be a split second where the file is available to be written to by script 1. So is there a way to open the file for reading / writing, and completely blank it out after reading is complete? I also thought about doing a file_get_contents to read it with script 2, then opening it in "w" mode, but I think it could still be written to again between the file_get_contents and the fopen. Here's some code, thanks in advance for any suggestions Script 1: runs frequently throughout the day at random times <?php echo "working"; if ( $file = fopen( "orders.txt", "w" ) ) { if ( flock( $file, LOCK_EX ) ) { //write some stuff } else { echo "<br>couldn't acquire lock..."; } } else { echo "<br>couldn't open file..."; } fclose( $file ); echo "done"; ?> Script 2: runs twice a day on schedule <?php if ( $orders = fopen( "orders.txt", "r+" ) ) { // open for reading and writing if ( flock( $orders, LOCK_EX ) ) { // file is locked, we can read it then erase it // first read the whole file while ( !feof( $orders ) ) { $allOrders .= fgets( $orders, 4096 ); } // here is where we want to erase the file and close it } else { echo "<br>could not lock orders.txt"; } } else { echo "<br>cannot open orders.txt"; } fclose( $orders ); ?> Quote Link to comment https://forums.phpfreaks.com/topic/203197-truncate-a-file-that-is-already-open/ Share on other sites More sharing options...
kenrbnsn Posted May 28, 2010 Share Posted May 28, 2010 You want to use file locking, flock so that script 2 has exclusive access to the file when you do that operation. Ken Quote Link to comment https://forums.phpfreaks.com/topic/203197-truncate-a-file-that-is-already-open/#findComment-1064651 Share on other sites More sharing options...
salathe Posted May 28, 2010 Share Posted May 28, 2010 For truncating the file, you'll want to use ftruncate on the open file handle, rather than closing it and opening again in write mode. Quote Link to comment https://forums.phpfreaks.com/topic/203197-truncate-a-file-that-is-already-open/#findComment-1064684 Share on other sites More sharing options...
Mike521 Posted May 28, 2010 Author Share Posted May 28, 2010 thanks salathe, that looks perfect! kenrbnsn, that's what I was doing? As it turned out I decided to use a database and have a separate column called "processed" that I set when the orders have been processed. Only drawback is I'll have to clean the DB once a week or so, but I prefer this method over the flat files Quote Link to comment https://forums.phpfreaks.com/topic/203197-truncate-a-file-that-is-already-open/#findComment-1064704 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.