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";
});
$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;
}