tags:

views:

58

answers:

2

Well actually, I already know how to write a file, but how do I write data to a file where the newest data added is at the top? I know how to do this, but the newest information is at the bottom. By the way, just to clarify, I do need all of the data to be displayed, not just the newest one. Thanks.

+2  A: 

Do you mean append or prepend? This will append:

$fp = fopen('data.txt', 'w');
fwrite($fp, '1');
fwrite($fp, '23');
fclose($fp);

This will prepend:

$handle = fopen('file.txt', "r+");
$read = fread($handle, filesize($file));
$data = $newStuff . $read;
if (!fwrite($handle, $data)) {
    echo 'fail';
} else {
    echo 'success';
}
karim79
A: 

If you want to append some text to the file content it's better to use another function:

file_put_contents('data.txt', '123', FILE_APPEND)
Konstantin Leboev