我和我的同事正在开发一个基于Flash的小游戏的聊天应用程序。我们希望通过在聊天文件达到一定限制后自动删除旧文本来使聊天文件尽可能小。假设文件超过50行,我们希望删除现有信息,并从第1行重新开始。这可能吗?
<?php
$file = "saved.txt";
$edited_text = $_POST['new_text'];
$open = fopen($file, "a+");
fwrite($open, "\n" . $edited_text);
fclose($open);
?>发布于 2014-06-07 01:56:03
基本上是这样的:
$lines = file('saved.txt');
$lines[] = 'new line of text';
array_unshift($lines); // remove first array element
file_put_contents('saved.txt', implode(PHP_EOL, $lines));发布于 2014-06-07 02:27:42
这将会起作用:
// Read entire file
$lines = file_get_contents('saved.txt');
// Add what you want to the beginning of array
array_unshift($lines, 'new line of text');
// Keep first 50 items
$lines = array_splice($lines, 0, 50);
// Write them back
file_put_contents('saved.txt', implode(PHP_EOL, $lines));将始终保持前50个元素不变(包括从新到旧的消息)。
https://stackoverflow.com/questions/24087910
复制相似问题