0

I am trying to find a method of overwriting (rather than appending) portions of similar text to a .txt file. I have an html user interface (Config) that uses a form to submit various bits of data to a php script that then writes a summary of this data to a .txt file that is read by another html script (Data Viewer) and displayed on a second user interface. There are occasions when the same data IDs will be written to the .txt file by the Config script, however, the Data Viewer GUI always reads the first instance of this data so misses the latest update. I don't really want to clear the whole text file when i hyperlink between the two GUIs if I can help it but I can't seem to find anywhere that explains how to overwrite text in a .txt file, they all seem to append. Below is a sample of the .txt file.

Analogue Output Channels: AN1_OUT,10,0,0,AN3_OUT,45,0,0,0,0,0,0,0,0,0,0,
Analogue Output Channels: AN1_OUT,10,0,0,AN3_OUT,45,0,20,0,0,0,0,0,0,0,0,
Analogue Output Channels: 
AN1_OUT,10,AN2_OUT,0,AN3_OUT,45,AN4_OUT,20,AN5_OUT,0,AN6_OUT,0,AN7_OUT,0,AN8_OUT,0,
Analogue Output Channels:
AN1_OUT,0,AN2_OUT,10,AN3_OUT,45,AN4_OUT,20,AN5_OUT,12,AN6_OUT,15,AN7_OUT,23,AN8_OUT,17,
Analogue Output Channels: 
AN1_OUT,0,AN2_OUT,10,AN3_OUT,45,AN4_OUT,20,AN5_OUT,12,AN6_OUT,15,AN7_OUT,23,AN8_OUT,17,
Analogue Output Channels:
AN1_OUT,1,AN2_OUT,10,AN3_OUT,45,AN4_OUT,20,AN5_OUT,12,AN6_OUT,15,AN7_OUT,23,AN8_OUT,17,

The write is pretty basic in the php script:

$fp = fopen("Log.txt","a");
fwrite($fp, "Analogue Output Channels: ");
fwrite($fp, "$var5,");

The writing of the variables is placed in a loop so the above is not the full code listing. Any advice would be appreciated.

AimSkyward
  • 145
  • 3
  • 12

2 Answers2

0

You need to change your fopen() call. Try $fp = fopen("Log.txt","w");

'w' - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

( source )

Skrivener
  • 1,003
  • 6
  • 11
  • I missed this "I don't really want to clear the whole text file" the first time around. The command I provided will wipe the file, so is not what you need. @Clickbeetle's answer is probably about what you need, also there's a very similar question for your purposes at http://stackoverflow.com/questions/3004041/how-to-replace-a-particular-line-in-a-text-file-using-php with several approaches depending on your system constraints. – Skrivener Nov 15 '13 at 00:49
0

I don't understand where is ID in your output, but for example if you have such txt file:

ID1 Some text
ID2 Some text
ID3 Some text

And then you want to ovewrite string ID2 Some text, the use this code:

$id = 'ID2';
$pattern = "/(?m)(^$id.*$)/";
$text = file_get_contents( 'Log.txt' );
$text = preg_replace( $pattern, $with_what_to_replace, $text );
file_put_contents( 'Log.txt' );

But as Dagon said it is better to use DataBase for this, if you don't want lines with the same ID to repeat. Writing to txt file is good for logging, where information can repeat.

Clickbeetle
  • 629
  • 4
  • 13
  • Unfortunately database can't be used, this is out of my control so I am stuck trying to get the text file to work. Sorry for the confusion about the ID's, I meant ID such as 'Analogue Output Channels' for instance but there are many more in the full text file. I will try your recommendation. – AimSkyward Nov 14 '13 at 21:36