operations with files and arrays 1. this function adds 1 line to the bottom of the file is it possible to add lines to the top? $gbfile='gb.txt'; function add($str){ global $gbfile; $tmp = trim($str); $fp=fopen($gbfile,'a+'); flock($fp, LOCK_EX); fwrite($fp, $tmp. "\n"); flock($fp, LOCK_UN); fclose($fp); } 2. there is an array consisting of text lines $lines = file ('entries.txt'); how to reverse an array (make first line last, last first and so on) 3. this function adds 1 line to the bottom of the file how to rewrite a file (remove contents of a file and write information into a file)? $gbfile='gb.txt'; function add($str){ global $gbfile; $tmp = trim($str); $fp=fopen($gbfile,'a+'); flock($fp, LOCK_EX); fwrite($fp, $tmp. "\n"); flock($fp, LOCK_UN); fclose($fp); }
To add lines to the top of a file, use "r+" instead of "a+" in your fopen(). To reverse the order of an array, use $newarray = array_reverse($oldarray, TRUE) . Make sure you have TRUE otherwise your keys won't be preserved. For the last one, are you talking about simply overwriting a file's contents? If so, use "w+" instead of "a+" in fopen().
Argh, I forgot to mention that ... I had such a witty remark too, "Brought to you in part by PHP.net" LOL