Reads a line in the file, ending with the current position.
This function automatically skips the empty lines and do not include the line return in result value.
resource $file The file resource, with the pointer placed at the end of the line to read:
mixed A string representing the line or FALSE if beginning of file is reached
protected function readLineFromFile($file) {
if (ftell($file) === 0) {
return false;
}
fseek($file, -1, SEEK_CUR);
$str = '';
while (true) {
$char = fgetc($file);
if ($char === "\n") {
// Leave the file with cursor before the line return
fseek($file, -1, SEEK_CUR);
break;
}
$str = $char . $str;
if (ftell($file) === 1) {
// All file is read, so we move cursor to the position 0
fseek($file, -1, SEEK_CUR);
break;
}
fseek($file, -2, SEEK_CUR);
}
return $str === '' ? $this
->readLineFromFile($file) : $str;
}