将文件内容追加到顶部而不是底部


Append file content to the top instead of bottom?

我有一个表格,工作人员可以填写该表格,将他们的消息发送到我们的更新页面。我的代码是

$filename = "files/armaupdates";
$text = $_POST['update'];
$updatesep = "<hr><br><hr><br>";
$fp = fopen($filename, "a+");
if($fp){
	fwrite($fp, $text);
	fwrite($fp, $updatesep);
	fclose($fp);
	echo "Updates has been written!";
}
else {
	echo "Error!";
}

我快疯了,因为我希望文本位于文件的顶部而不是底部,有人吗?

$filename = "files/armaupdates";
$text = $_POST['update'];
$updatesep = "<hr><br><hr><br>";
$content = file_get_contents($filename);
$fp = fopen($filename, "w");
if($fp){
    fwrite($fp, ($text . $updatesep . $content));
    fclose($fp);
    echo "Updates has been written!";
}
else {
    echo "Error!";
}
只需在

内容前面加上新数据:

$updatedContents = $text . $updatesep . file_get_contents($filename);

为您提供以下代码:

$filename = "files/armaupdates";
$text = $_POST['update'];
$updatesep = "<hr><br><hr><br>";
$updatedContents = $text . $updatesep . file_get_contents($filename);
$fp = fopen($filename, "a+");
if($fp){
    fwrite($fp, $updatedContents);
    fclose($fp);
    echo "Updates has been written!";
}
else {
    echo "Error!";
}