Jump to content

how do I write to the end of the first line of a flat text file


husslela03

Recommended Posts

Hello,

 

I would like to write to the end of the first line of flat text file.

 

The text I would like to append is text that is entered from a text box on a form.

 

How would i do this?

 

I read the flat file into an array, but I wasn't sure if I need to do a loop to write to the first line or?

 

Please help...

I read the flat file into an array, but I wasn't sure if I need to do a loop to write to the first line or?

Yes you can use the file function to grab the contents of each line into an array. But you wouldn't need to use a loop to append some text to the first line. You'd use $var[0] eg

$contents = file('filename.txt');

// append something to Line 1
$content[0] .= " whatever you want to put here";

 

Now to save the changes you'd use

// convert the array to a string using implode()
$text = implode($content);

// rewrite the file
file_put_contents("filename.txt", $text);

Archived

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

×
×
  • 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.