PHP code example of mb4it / filesystem

1. Go to this page and download the library: Download mb4it/filesystem library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mb4it / filesystem example snippets




use MB\Filesystem\Filesystem;
use MB\Filesystem\Exceptions\FileNotFoundException;
use MB\Filesystem\Exceptions\IOException;

$filesystem = new Filesystem(__DIR__ . '/storage');

// Write file
$filesystem->put('example.txt', 'Hello, world!');

// Read file content
try {
    $content = $filesystem->content('example.txt');
} catch (FileNotFoundException $e) {
    // file not found
} catch (IOException $e) {
    // other I/O error
}

$filesystem = new Filesystem(__DIR__ . '/storage');

// Write array as JSON
$filesystem->putJson('config.json', ['debug' => true]);

// Read JSON as array
$config = $filesystem->json('config.json', true);

// Read JSON or return default value if file is missing
$config = $filesystem->json('missing.json', true, ['debug' => false]);

// Update JSON via read‑modify‑write
$filesystem->updateJson('config.json', static function (array $data): array {
    $data['debug'] = false;
    return $data;
});

// Read content, or default if file is missing
$text = $filesystem->content('log.txt', '');

// Read-modify-write (creates file with empty content if missing). Atomic by default.
$filesystem->updateContent('log.txt', static function (string $current): string {
    return $current . "New line\n";
});

// Non-atomic (faster) when crash during write is acceptable
$filesystem->updateContent('cache.txt', static function (string $c): string {
    return $c . "entry\n";
}, false);

// Or pass a File node when you already have one
$file = $filesystem->file('log.txt');
$filesystem->updateContent($file, static function (string $current): string {
    return $current . "Appended\n";
});

$node = $filesystem->get('path/to/something');

if ($node instanceof \MB\Filesystem\Nodes\File) {
    $text = $node->content();
} elseif ($node instanceof \MB\Filesystem\Nodes\Directory) {
    $list = $node->files(true);
} elseif ($node instanceof \MB\Filesystem\Nodes\Link) {
    $resolved = $node->resolve();  // File|Directory|null
}

$file = $filesystem->file('path/to/file.txt');

$file->content();           // full content (string)
$file->line(42);            // 42nd line (1-based)
foreach ($file->lines() as $i => $line) { /* line-by-line, memory-efficient */ }
$file->update(fn (string $c) => $c . "\nnew line");  // optional second arg: $atomic = true
$file->write('new content');
$file->delete();
$file->move('other.txt');
$file->copy('backup.txt');
$file->chmod(0644);
$file->touch();             // or touch($mtime)

$dir = $filesystem->directory('path/to/dir');

$dir->files(true);          // list files, recursive
$dir->directories(true);   // list subdirs
$dir->create(0755, true);  // ensure exists
$dir->delete(true);        // delete recursively
$dir->chmod(0755);
$dir->touch($mtime);

$filesystem->put('target.txt', 'content');
$filesystem->createSymlink('target.txt', 'link.txt');  // link path must not exist

if ($filesystem->isLink('my-link')) {
    $link = $filesystem->link('my-link');
    $link->target;       // target path
    $link->isBroken();   // true if target does not exist
    $resolved = $link->resolve();  // File|Directory|null
}
$links = $filesystem->links('dir', true);  // list symlinks, optional recursive

$filesystem->move('old.txt', 'new.txt');
$filesystem->rename('olddir', 'newdir');   // rename directory
$canonical = $filesystem->realPath('config/../logs/app.log');

$filesystem = new Filesystem(__DIR__ . '/storage');

// Create directory (including parents)
$filesystem->makeDirectory('logs/app');

// Get files / directories
$files = $filesystem->files('logs', true);        // recursive
$dirs  = $filesystem->directories('logs', true);  // recursive

// Recursive copy and delete
$filesystem->copyDirectoryRecursive('logs', 'logs_backup');
$filesystem->deleteDirectory('logs_backup', true);

$filesystem = new Filesystem(__DIR__ . '/storage');

// All .log files (with recursion)
$logFiles = $filesystem->filesWithExtension('logs', 'log', true);

// Files by name mask
$appLogs = $filesystem->filesByPattern('logs', 'app*.log', true);

$filesystem = new Filesystem(__DIR__ . '/storage');

$filesystem->put('file.txt', '12345');

$size = $filesystem->size('file.txt');          // 5
$mtime = $filesystem->lastModified('file.txt'); // UNIX timestamp

// Permissions and mtime
$filesystem->chmod('file.txt', 0644);
$filesystem->touch('file.txt', $mtime);         // or null for current time

// Append
$filesystem->append('file.txt', '678');

// Atomic write
$filesystem->putAtomic('file.txt', 'new content');

$filesystem = new Filesystem(__DIR__ . '/storage');

// Paths will be resolved relative to __DIR__ . '/storage'
$filesystem->put('foo/bar.txt', 'content');

// Path helpers
$path = $filesystem->join('foo', 'bar', 'baz.txt');   // foo/bar/baz.txt
$norm = $filesystem->normalize('./foo/../bar/test');  // bar/test
$rel  = $filesystem->relative('/var/www/app', '/var/www'); // app

use MB\Filesystem\Filesystem;
use MB\Filesystem\Finder\ClassFinder;

$filesystem = new Filesystem(); // you can pass basePath if needed
$finder = new ClassFinder($filesystem);

// Find all classes that extend App\BaseClass
$byExtends = $finder->extends(__DIR__ . '/src', App\BaseClass::class);

// Find all classes that implement App\MyInterface
$byImplements = $finder->implements(__DIR__ . '/src', App\MyInterface::class);

// Find all classes that use App\MyTrait
$withTrait = $finder->hasTrait(__DIR__ . '/src', App\MyTrait::class);

foreach ($byImplements as $info) {
    echo $info['class'] . ' defined in ' . $info['file'] . PHP_EOL;
}

$filesystem = new Filesystem(__DIR__);

// Paths only (lighter)
$paths = $filesystem->grepPaths('bitrix/templates', '$APPLICATION->IncludeComponent(', [
    'extensions' => ['php'],
]);

// File nodes (for metadata / content)
$files = $filesystem->grep('bitrix/templates', '$APPLICATION->IncludeComponent(', [
    'extensions' => ['php'],
]);
foreach ($files as $file) {
    echo $file->path . PHP_EOL;
    $content = $file->content();
}

// Regex and case-insensitive
$paths = $filesystem->grepPaths('bitrix', '/IncludeComponent\s*\(/', [
    'regex' => true,
    'extensions' => ['php'],
    'case_sensitive' => false,
]);

// Find all component.php files (paths)
$paths = $filesystem->findPaths('bitrix', [
    'name' => 'component.php',
    'type' => 'file',
]);

// Find all directories named templates
$dirs = $filesystem->find('bitrix', [
    'name' => 'templates',
    'type' => 'dir',
]);

// Find by shell-like pattern and limit depth
$phpFiles = $filesystem->findPaths('src', [
    'name_pattern' => '*.php',
    'type' => 'file',
    'max_depth' => 2,
]);

try {
    $filesystem->put('config/settings.php', ' return [];');
} catch (PermissionException $e) {
    // no write permissions
} catch (IOException $e) {
    // other I/O error
}
bash
php benchmark/run.php