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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.