Jump to content

[SOLVED] Writing to a .txt file


npsari

Recommended Posts

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

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);
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.