Skip to content
Search
Examples

Filesystem

Description

fstab()->for('file') is the local filesystem every reader and writer goes through. It writes streams, lists paths by glob pattern and reads files back, in full or in chunks.

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