Jump to content

reading and overwriting text file


kensbeijing

Recommended Posts

I've been trying this over and over but still no luck. I have a text file which basically contains 3 numbers:111What I want to do is read the file, take the top line add one to it and overwrite so it becomes211

$alert_line = array();$alert_file = 'internal/alerts.txt';			$alert_txt = fopen($alert_file, 'r+');			while(!feof($alert_txt))			{				$alert_line[] = fgets($alert_txt);			}			//fclose($alert_txt);			$alert_orders = $alert_line[0] + 1;			//$alert_txt = fopen($alert_file, 'r+');			fwrite($alert_txt, $alert_orders."\n".$alert_line[1]."\n".$alert_line[2]);			fclose($alert_txt);			?>

At the moment my code gives me something like111211Please help thank you

Link to comment
Share on other sites

It will be easier if you just read the file with the file() function and implode() the whole thing in the end.Perhaps something like:

$alert_txt = file($alert_file, FILE_IGNORE_NEW_LINES);$alert_txt[0]++;file_put_contents(implode('\n', $alert_txt));

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...