题
我猜它是与fgets,但我无法找到特定的语法。我想读出(在一个字符串我想是更容易)的最后一行添加到一个日志文件。
解决方案
最简单的幼稚的解决方案是简单地:
$file = "/path/to/file";
$data = file($file);
$line = $data[count($data)-1];
虽然,这将整个文件加载到存储器中。可能是一个问题(或没有)。更好的解决方案是这样的:
$file = escapeshellarg($file); // for the security concious (should be everyone!)
$line = `tail -n 1 $file`;
其他提示
这看起来是你在找什么:
它实现一个使用FSEEK()具有负指数卷起从端部的文件的功能。你可以定义你想要多少行归还。
该代码还可以如在GitHub 要旨:
// full path to text file
define("TEXT_FILE", "/home/www/default-error.log");
// number of lines to read from the end of file
define("LINES_COUNT", 10);
function read_file($file, $lines) {
//global $fsize;
$handle = fopen($file, "r");
$linecounter = $lines;
$pos = -2;
$beginning = false;
$text = array();
while ($linecounter > 0) {
$t = " ";
while ($t != "\n") {
if(fseek($handle, $pos, SEEK_END) == -1) {
$beginning = true;
break;
}
$t = fgetc($handle);
$pos --;
}
$linecounter --;
if ($beginning) {
rewind($handle);
}
$text[$lines-$linecounter-1] = fgets($handle);
if ($beginning) break;
}
fclose ($handle);
return array_reverse($text);
}
$fsize = round(filesize(TEXT_FILE)/1024/1024,2);
echo "<strong>".TEXT_FILE."</strong>\n\n";
echo "File size is {$fsize} megabytes\n\n";
echo "Last ".LINES_COUNT." lines of the file:\n\n";
$lines = read_file(TEXT_FILE, LINES_COUNT);
foreach ($lines as $line) {
echo $line;
}
define('YOUR_EOL', "\n");
$fp = fopen('yourfile.txt', 'r');
$pos = -1; $line = ''; $c = '';
do {
$line = $c . $line;
fseek($fp, $pos--, SEEK_END);
$c = fgetc($fp);
} while ($c != YOUR_EOL);
echo $line;
fclose($fp);
这是更好的,因为它不完整的文件加载到存储器...
设置YOUR_EOL你的正确路线的结局,如果你使用相同的行尾为你的脚本所在的OS的默认行结束符,你可以使用常量PHP_EOL。
function seekLastLine($f) {
$pos = -2;
do {
fseek($f, $pos--, SEEK_END);
$ch = fgetc($f);
} while ($ch != "\n");
}
-2
因为最后一个字符可以\n
您要么必须读取行线的文件,并保存最后读线得到它。
或者,如果在Unix / Linux,你可以考虑使用shell命令尾
tail -n 1 filename
这一个不会打破一个1个或0行文件。
function readlastline($fileName)
{
$fp = @fopen($fileName, "r");
$begining = fseek($fp, 0);
$pos = -1;
$t = " ";
while ($t != "\n") {
fseek($fp, $pos, SEEK_END);
if(ftell($fp) == $begining){
break;
}
$t = fgetc($fp);
$pos = $pos - 1;
}
$t = fgets($fp);
fclose($fp);
return $t;
}
...为什么刚才读的最后一行?
function readLines($fp, $num) {
$line_count = 0; $line = ''; $pos = -1; $lines = array(); $c = '';
while($line_count < $num) {
$line = $c . $line;
fseek($fp, $pos--, SEEK_END);
$c = fgetc($fp);
if($c == "\n") { $line_count++; $lines[] = $line; $line = ''; $c = ''; }
}
return $lines;
}
$filename = "content.txt";
$fp = @fopen($filename, "r");
print_r(readLines($fp, 2));
fclose($fp);
如果您想逐行读取 file
函数读取文件,一行行的内容,并返回各行作为数组的一个元素。
所以,你可以做一些简单的像:
$lines = file('log.txt');
$lastLine = array_pop($lines);
@unique_stephen,你的答案是有缺陷的。 PHP FSEEK失败返回0成功,-1。结果存储在$ begining(原文如此),再后来在FTELL过滤器使用它()是不正确的。如果我的信誉较高,我会投你失望,并给出了评语。下面是unique_stephen的功能的修改版本。
function readlastline($fileName)
{
$fp = @fopen($fileName, "r");
if (fseek($fp, 0) == -1)
exit('Cannot seek to beginning of the file');
$pos = -1;
$t = " ";
while ($t != "\n") {
if (fseek($fp, $pos, SEEK_END) == -1)
exit('Cannot seek to the end of the file');
if (ftell($fp) == 0) {
break;
}
$t = fgetc($fp);
$pos = $pos - 1;
}
$t = fgets($fp);
fclose($fp);
return $t;
}
请注意:PHP的FSEEK不能设法寻求比PHP_MAX_INT较大的文件的末尾这是32位的,即使签署了64位二进制代码
。不隶属于 StackOverflow