Skip to content
Search
Examples

Filesystem

Description

Flow includes a dedicated filesystem abstraction for working with files. The local filesystem provides operations for listing files (with glob pattern support), reading data (in full or chunks), and writing data.

Documentation

Every reader and writer reaches storage through the filesystem abstraction.

Code

<?php

declare(strict_types=1);

use function Flow\Filesystem\DSL\{fstab, path};

require __DIR__ . '/vendor/autoload.php';

$filesystem = fstab()->for('file');

foreach (['first.txt' => "one\n", 'second.txt' => "two\n"] as $name => $content) {
    $stream = $filesystem->writeTo(path(__DIR__ . '/listing/' . $name));
    $stream->append($content);
    $stream->close();
}

$listing = $filesystem->writeTo(path(__DIR__ . '/listing/written.txt'));

$listing->append("Files List\n\n");

foreach ($filesystem->list(path(__DIR__ . '/listing/*.txt')) as $file) {
    $listing->append(($file->isFile() ? 'File' : 'Directory') . ': ' . $file->path->basename() . "\n");
}

$listing->close();

echo $filesystem->readFrom(path(__DIR__ . '/listing/written.txt'))->content();
Contributors

Built in the open.

Join us on GitHub
scroll back to top