tivrfoa Posted January 24, 2009 Share Posted January 24, 2009 hello fopen($handle, "a") works to append text in the end of the file. can someone show how to append in the beggining of the file? thanks very much Quote Link to comment Share on other sites More sharing options...
.josh Posted January 24, 2009 Share Posted January 24, 2009 unfortunately there is not a built in way to do that. You have to basically grab the contents of the file and put it into a string or array and concat the new stuff to the string or array_unshift it to the array and rewrite the whole thing back to the file. Quote Link to comment Share on other sites More sharing options...
tivrfoa Posted January 24, 2009 Author Share Posted January 24, 2009 unfortunately there is not a built in way to do that. You have to basically grab the contents of the file and put it into a string or array and concat the new stuff to the string or array_unshift it to the array and rewrite the whole thing back to the file. thanks! php core could create this =D this is what I did and it's working: $text = 'something'; $filename = 'your_file.txt'; $text .= file_get_contents($filename); $f = fopen($filename, 'w') or die("can't open file"); fwrite($f, $text); fclose($f); Quote Link to comment Share on other sites More sharing options...
.josh Posted January 24, 2009 Share Posted January 24, 2009 dunno what format you're wanting to make your file overall, but you might consider adding a newline char on the end of the text you want to pre-pend. Quote Link to comment 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.