PHP - ファイル内の最後の行を返しますか?
質問
私はそれが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`;
他のヒント
これが見えます:
tekkie.flashbit.net:PHPでのテール機能する
これは、端からファイルをロールアップするために負の屈折率との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上で使用すると、シェルコマンドの尾を使用して検討するかもしれない場合は、
tail -n 1 filename
この1は文句を言わない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、あなたの答えは欠陥があります。失敗を成功のための0と-1を返しfseekはPHP。 $の初め(原文のまま)に結果を保存し、後で)(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にも、64ビットのバイナリに署名した32ビットであるPHP_MAX_INTを超えるファイルの末尾に求めることを管理することはできません。