这是插入文件,一次放入 1 行。例如,我只想读取最后 10 行。
$file = SITE_ROOT."logs/log.txt";
if ($handle = fopen($file, 'a+b')) {
$content = Session::get('username') . " has logged out on - " . datetime_to_text("now") . "\r\n";
fwrite($handle, $content);
fclose($handle);
} else {
echo 'Culd not open file for writing.';
}
这就是我阅读它们的方式,阅读它们。
$file = SITE_ROOT . "logs/log.txt";
$content = "";
if ($handle = fopen($file, 'r')) {
while (!feof($handle)) {
$content .= '<li><a href="#">' . fgets($handle) . '</a></li>';
echo count($handle);
}
fclose($handle);
}
echo nl2br($content);
怎么读只有10?
完成,我找到了我正在寻找的答案。
Thx, i found useffull this one.
$file = SITE_ROOT . "logs/log.txt";
$data = array_slice(file($file), -10);
$data1 = file($file);
foreach ($data as $line) {
echo '<li><a href="#">'. nl2br($line) . '</a></li>';
}