husslela03 Posted December 5, 2009 Share Posted December 5, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/184109-how-do-i-write-to-the-end-of-the-first-line-of-a-flat-text-file/ Share on other sites More sharing options...
wildteen88 Posted December 5, 2009 Share Posted December 5, 2009 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); Quote Link to comment https://forums.phpfreaks.com/topic/184109-how-do-i-write-to-the-end-of-the-first-line-of-a-flat-text-file/#findComment-972049 Share on other sites More sharing options...
husslela03 Posted December 5, 2009 Author Share Posted December 5, 2009 I'm getting a fatal error regarding file_put_contents. Fatal error: Call to undefined function: file_put_contents() in C:\xampp\htdocs\SASI\add_assignment.php on line 56 Quote Link to comment https://forums.phpfreaks.com/topic/184109-how-do-i-write-to-the-end-of-the-first-line-of-a-flat-text-file/#findComment-972079 Share on other sites More sharing options...
wildteen88 Posted December 5, 2009 Share Posted December 5, 2009 What version of PHP are you using? run phpversion or phpinfo to find out. Quote Link to comment https://forums.phpfreaks.com/topic/184109-how-do-i-write-to-the-end-of-the-first-line-of-a-flat-text-file/#findComment-972080 Share on other sites More sharing options...
husslela03 Posted December 5, 2009 Author Share Posted December 5, 2009 4.4.9 Quote Link to comment https://forums.phpfreaks.com/topic/184109-how-do-i-write-to-the-end-of-the-first-line-of-a-flat-text-file/#findComment-972093 Share on other sites More sharing options...
Alex Posted December 5, 2009 Share Posted December 5, 2009 file_put_contents is only available in PHP 5+, instead you'll have to use fopen, fwrite and fclose. $fp = fopen('somefile.txt', 'w'); fwrite($fp, $text); fclose($fp); Quote Link to comment https://forums.phpfreaks.com/topic/184109-how-do-i-write-to-the-end-of-the-first-line-of-a-flat-text-file/#findComment-972104 Share on other sites More sharing options...
mrMarcus Posted December 5, 2009 Share Posted December 5, 2009 or upgrade to the latest version, which would be the more ideal solution. keep with the times. Quote Link to comment https://forums.phpfreaks.com/topic/184109-how-do-i-write-to-the-end-of-the-first-line-of-a-flat-text-file/#findComment-972113 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.