Jump to content

Recommended Posts

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 );

?>

Link to comment
https://forums.phpfreaks.com/topic/203197-truncate-a-file-that-is-already-open/
Share on other sites

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.