File::stream()

Streams the file content chunk by chunk.

Table of Contents
  1. Description
  2. Example

Description

File::stream(?int $max = 1024): Traversable;

This method streams the file content chunk by chunk to prevent memory from exceeding the limit when using File::content() to read large file content. In most cases, you may just want to check for the presence of certain characters in the file content before doing the next tasks.

The default chunk limits and assumes that each line has 1024 bytes of characters. It will stop at the line break character when it reaches it, even when the maximum line length found is less than 1024 bytes.

Example

$file = new File('.\path\to\file.txt');

// Search for “TODO:” text in the file content
$found = false;
foreach ($file->stream() as $k => $v) {
    if (false !== strpos($v, 'TODO:')) {
        $found = true;
        break;
    }
}

if ($found) {
    // The file contains “TODO:” text!
}

File::size()

Gets the file sizes in human readable string format.

File::stream()

Streams the file content chunk by chunk.