我只想打开一个带有fopen()
的rsyslog文件,用前3行设置变量,用这3行中的最后一行设置变量。然后采取其他3条线路e.t.c.
$path_file = variable_get('$path');
$file = fopen($path_file, 'r');
for($i=0;$i<3;$i++) {
$line = fgets($file);
$line = variable_set($line);
}
fclose($file);
改为使用file()
(将所有内容作为数组读取)
试试这个:
$file = file('$path');
for($x = 0; $x < count($file); $x = $x + 3)
{
if(isset($file[$x]) && isset($file[$x +1]) && isset($file[$x + 2])
{
//do something with the values.
}
}
function readFileStartingAtLineNumber($x)
{
$file = file('$path');
if(isset($file[$x]) && isset($file[$x +1]) && isset($file[$x + 2])
{
//do something with the values.
}
}
function getLog($path, $numberOfLines, $lastIndex) {
$file = fopen($path, 'r');
if (!$file) {
print 'error opening file';
}
else {
$data = '';
$i = -1;
while(($line = fgets($file)) !== FALSE) {
if(++$i < $lastIndex) continue;
if($numberOfLines-- == 0) break;
$data .= $line;
}
fclose($file);
if ($data === '') {
print 'EOF reached without getting data';
}
}
return $i;
}