kratsg Posted September 1, 2008 Share Posted September 1, 2008 Ok, I've got an interesting question. Is there such thing as a "backwards" append? By backwards, instead of appending to the END of the file, it appends to the BEGINNING? Yes, I know you can do something like $message = sanitize($_POST['message'])."\n".file_get_contents("some_file.txt"); $fp = fopen("some_file.txt","w"); fwrite($fp,$message); fclose($fp); And the end result would be the same. However, I was wondering if there was a way to do it WITHOUT having to read out the whole file and overwriting that same file? (and why in the world couldn't PHP have a backwards append? They've already got one to add to the end of the file, why not to the beginning?) Quote Link to comment https://forums.phpfreaks.com/topic/122176-fwrite-is-there-any-other-way/ Share on other sites More sharing options...
kenrbnsn Posted September 1, 2008 Share Posted September 1, 2008 The general way of doing this is: 1) read the entire file into an array 2) reverse the array 3) add the line you want to put at the beginning of the file at the end of the array 4) reverse the array 5) write it out <?php $file_array = map_array('trim',file('yourfile.here')); $file_array = array_reverse($file_array); $file_array[] = 'The line goes here'; file_put_contents('yourfile.here',implode("\n",array_reverse($file_array))); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/122176-fwrite-is-there-any-other-way/#findComment-630756 Share on other sites More sharing options...
kratsg Posted September 1, 2008 Author Share Posted September 1, 2008 Does file_put_contents() truncate the file before inserting the new data? Quote Link to comment https://forums.phpfreaks.com/topic/122176-fwrite-is-there-any-other-way/#findComment-630964 Share on other sites More sharing options...
JasonLewis Posted September 1, 2008 Share Posted September 1, 2008 The manual is your friend. To answer your question though, yes. It overwrites the existing file, unless you set the optional flag which just appends the data. Quote Link to comment https://forums.phpfreaks.com/topic/122176-fwrite-is-there-any-other-way/#findComment-630968 Share on other sites More sharing options...
Fadion Posted September 1, 2008 Share Posted September 1, 2008 @Ken, the function should be array_map() Quote Link to comment https://forums.phpfreaks.com/topic/122176-fwrite-is-there-any-other-way/#findComment-630972 Share on other sites More sharing options...
JasonLewis Posted September 1, 2008 Share Posted September 1, 2008 @Ken, the function should be array_map() Ahh the silliness of the PHP API. They throw things all around the joint they do. Quote Link to comment https://forums.phpfreaks.com/topic/122176-fwrite-is-there-any-other-way/#findComment-630978 Share on other sites More sharing options...
Fadion Posted September 1, 2008 Share Posted September 1, 2008 Ahh the silliness of the PHP API. They throw things all around the joint they do. lol. The array functions all start with array_, but there are ones like "array_walk_recursive" or "move_uploaded_file" which just don't make sense. Quote Link to comment https://forums.phpfreaks.com/topic/122176-fwrite-is-there-any-other-way/#findComment-630987 Share on other sites More sharing options...
kenrbnsn Posted September 1, 2008 Share Posted September 1, 2008 Sorry -- I wrote that at 1 AM... Ken Quote Link to comment https://forums.phpfreaks.com/topic/122176-fwrite-is-there-any-other-way/#findComment-630989 Share on other sites More sharing options...
JasonLewis Posted September 1, 2008 Share Posted September 1, 2008 Not all array functions start with array_. You have all the sort functions. It's not only functions, but the parameters functions take. I get so confused sometimes I keep on referring to the manual to ensure I am putting the correct parameter in the correct position. Quote Link to comment https://forums.phpfreaks.com/topic/122176-fwrite-is-there-any-other-way/#findComment-630992 Share on other sites More sharing options...
Fadion Posted September 1, 2008 Share Posted September 1, 2008 Not all array functions start with array_. You have all the sort functions. It's not only functions, but the parameters functions take. I get so confused sometimes I keep on referring to the manual to ensure I am putting the correct parameter in the correct position. I meant all functions that: strpos("some_array_function", "array") . Yeah actually it's really confusing remembering the parameters position or optional parameter constants. I suffered this (and a lot more) a bit while taking some simulation tests for the zend zce. It needs quite good memory, but at least it has the greatest documentation ever. Quote Link to comment https://forums.phpfreaks.com/topic/122176-fwrite-is-there-any-other-way/#findComment-631007 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.