Page MenuHomePhabricator

final class LinesOfALargeFile
libphutil Technical Documentation (Filesystem)

Read the lines of a file, one at a time. This allows you to process large files without holding them in memory. In most cases, it is more efficient (and certainly simpler) to read the entire file and explode() it. For more information, see LinesOfALarge. See also LinesOfALargeExecFuture, for a similar abstraction that works on executed commands.

foreach (new LinesOfALargeFile('/some/large/logfile.log') as $line) {
  // ...
}

If the file can not be read, a FilesystemException is thrown.

Tasks

Construction

  • public function __construct($file_name) — To construct, pass the path to a file.

Internals

  • public function __destruct() — Closes the file handle.
  • private function closeHandle() — Close the file handle, if it is open.
  • protected function willRewind() — Closes the file handle if it is open, and reopens it.
  • protected function readMore() — Read the file chunk-by-chunk.

Methods

public function __construct($file_name)

To construct, pass the path to a file.

Parameters
string$file_nameFile path to read.
Return
this//Implicit.//

public function __destruct()

Closes the file handle.

Return
void

private function closeHandle()

Close the file handle, if it is open.

Return
$this

protected function willRewind()

Closes the file handle if it is open, and reopens it.

Return
void

protected function readMore()

Read the file chunk-by-chunk.

Return
stringNext chunk of the file.