Skip to content

Read files

By chunks

use Innmind\IO\IO;
use Innmind\Url\Path;

$chunks = IO::fromAmbientAuthority()
    ->files()
    ->read(Path::of('/path/to/file.ext'))
    ->chunks(8192);

This will produce a lazy Sequence containing Str objects with each value with a length of at most 8192.

By lines

use Innmind\IO\IO;
use Innmind\Url\Path;

$chunks = IO::fromAmbientAuthority()
    ->files()
    ->read(Path::of('/path/to/file.ext'))
    ->lines();

This will produce a lazy Sequence containing Str objects. Each line ends with \n, except the last one.

With a specific encoding

By default when reading a file the Str produced uses the Str\Encoding::utf8. You can change that like this:

use Innmind\IO\IO;
use Innmind\Url\Path;
use Innmind\Immutable\Str;

$chunks = IO::fromAmbientAuthority()
    ->files()
    ->read(Path::of('/path/to/file.ext'))
    ->toEncoding(Str\Encoding::ascii)
    ->lines();