npsari Posted May 6, 2008 Share Posted May 6, 2008 Hi there, I use this simple code to store the searches made by my visitors $file = "search.txt"; $words = "$for \n\n"; $act = fopen($file, 'a') or die("Can't open file"); fwrite($act, $words); fclose($act); But the data is stored descending which means, that the new searches are stored at the bottom of the page so, to see what are the latest searches, i have to scroll down everytime Is it possible that the new data be saved at the top fo the old data so, the oldest will be at the bottom, and the newest will be at the top how Link to comment https://forums.phpfreaks.com/topic/104457-solved-writing-to-a-txt-file/ Share on other sites More sharing options...
blackcell Posted May 6, 2008 Share Posted May 6, 2008 You need to read the data out into an array $array, write the new search into the truncated file, then write the $array back in. If you need an example I will have to go digging but I think I still have some old code. Link to comment https://forums.phpfreaks.com/topic/104457-solved-writing-to-a-txt-file/#findComment-534725 Share on other sites More sharing options...
moselkady Posted May 6, 2008 Share Posted May 6, 2008 You can read the contents of the file, add the new data before it, write everything to a new temporary file, then copy that new file to the original one. <?php $file = "search.txt"; $file2 = "temp.txt"; $words = "$for \n\n"; $words .= file_get_contents($file); $act = fopen($file2, 'w') ; fwrite($act, $words); fclose($act); copy($file2, $file); ?> Link to comment https://forums.phpfreaks.com/topic/104457-solved-writing-to-a-txt-file/#findComment-534726 Share on other sites More sharing options...
npsari Posted May 6, 2008 Author Share Posted May 6, 2008 Thank you blacksell And thank you moselkady It is a smart idea I will go for it Link to comment https://forums.phpfreaks.com/topic/104457-solved-writing-to-a-txt-file/#findComment-534730 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.