joomla/filesystem
Joomla Framework filesystem utilities for common file operations. Includes helpers for safe filenames, uploads, and path handling, with a patcher component for applying file patches. Install via Composer and use in PHP apps needing lightweight filesystem tooling.
Installation:
composer require joomla/filesystem "^3.0"
For PHP 8.3+ projects (v4.x), use "^4.0".
First Use Case: Upload a file with validation:
use Joomla\Filesystem\File;
$file = request()->file('file');
$path = storage_path('app/uploads/' . File::makeSafe($file->getClientOriginalName()));
File::upload($file->getPathname(), $path);
Key Classes to Explore:
Joomla\Filesystem\File (file operations)Joomla\Filesystem\Folder (directory operations)Joomla\Filesystem\Path (path utilities)fileUploadMaxSize() for server config checks.use Joomla\Filesystem\File;
// Validate file
$allowedExtensions = ['jpg', 'png', 'pdf'];
$maxSize = 5 * 1024 * 1024; // 5MB
$file = request()->file('file');
$ext = strtolower(File::getExt($file->getClientOriginalName()));
if (!in_array($ext, $allowedExtensions)) {
throw new \InvalidArgumentException("Invalid file type.");
}
if ($file->getSize() > $maxSize) {
throw new \RuntimeException("File too large.");
}
// Upload
$safeName = File::makeSafe($file->getClientOriginalName());
$path = storage_path("app/uploads/{$safeName}");
File::upload($file->getPathname(), $path);
use Joomla\Filesystem\Folder;
// Create directory (recursive)
Folder::create(storage_path('app/cache'), 0755, true);
// List files (sorted)
$files = Folder::files(storage_path('app/uploads'), '.*\.jpg$', true, 'natsort');
use Joomla\Filesystem\File;
// Read/write text
$content = File::read(storage_path('app/config.php'));
File::write(storage_path('app/config.php'), $content . "\n// Updated");
// Read binary (e.g., images)
$imageData = File::read(storage_path('app/uploads/image.jpg'), null, null, true);
use Joomla\Filesystem\Path;
// Normalize and resolve paths
$absolutePath = Path::resolve(storage_path('..') . '/public');
$relativePath = Path::makeRelative($absolutePath, base_path());
Use with Laravel’s Storage Facade:
use Illuminate\Support\Facades\Storage;
use Joomla\Filesystem\File;
$file = request()->file('file');
$disk = Storage::disk('local');
$path = $disk->path('uploads/' . File::makeSafe($file->getClientOriginalName()));
File::upload($file->getPathname(), $path);
Custom Validation Rules:
use Joomla\Filesystem\File;
use Illuminate\Validation\Rule;
$validator = Validator::make($request->all(), [
'file' => [
'required',
'file',
Rule::function('ext', function ($attribute, $value) {
$ext = strtolower(File::getExt($value->getClientOriginalName()));
return in_array($ext, ['jpg', 'png']);
}),
],
]);
Service Provider Binding (for dependency injection):
// app/Providers/AppServiceProvider.php
public function register()
{
$this->app->bind('joomla.filesystem', function () {
return new \Joomla\Filesystem\File();
});
}
Artisan Commands:
use Joomla\Filesystem\Folder;
use Illuminate\Console\Command;
class CleanCacheCommand extends Command
{
protected $signature = 'cache:clean';
public function handle()
{
Folder::delete(storage_path('app/cache'));
$this->info('Cache cleared!');
}
}
PHP Version Mismatch:
^3.2 for compatibility.^3.2.Path Handling Quirks:
File::makeSafe() transliterates filenames (e.g., köln.jpg → koln.jpg).Path::resolve() for absolute paths to avoid issues with ../ or ./.Folder::create() fails silently if the parent directory doesn’t exist (use recursive: true).Error Messages:
try-catch:
try {
File::copy($source, $dest);
} catch (\Exception $e) {
Log::error("Copy failed: " . $e->getMessage());
throw new \RuntimeException("Failed to copy file.");
}
File Permissions:
Folder::create() defaults to 0755. Use 0777 for writable directories (not recommended for production).Folder::create($path, 0755);
chmod($path, 0777); // Only if absolutely necessary
Large File Handling:
File::read() loads entire files into memory. For large files (>100MB), use streams:
$handle = fopen($path, 'r');
while (!feof($handle)) {
$buffer = fread($handle, 8192);
// Process buffer
}
fclose($handle);
Windows Path Issues:
DIRECTORY_SEPARATOR or Path::normalize() for cross-platform paths:
$path = Path::normalize('folder' . DIRECTORY_SEPARATOR . 'file.txt');
Enable Debug Mode:
\Joomla\Filesystem\File::setDebug(true); // Logs operations to error log
Check File Existence:
File::exists() (v3.2+) or Folder::exists():
if (!File::exists($path)) {
throw new \RuntimeException("File not found: {$path}");
}
Common Issues:
storage and bootstrap/cache permissions (chmod -R 775 storage bootstrap/cache).Path::resolve() to get absolute paths and debug with realpath().File::makeSafe() before operations.Custom File Validator:
class CustomFileValidator
{
public static function validate(array $file, array $rules): bool
{
$ext = strtolower(File::getExt($file['name']));
return in_array($ext, $rules['extensions']) &&
$file['size'] <= $rules['max_size'];
}
}
Event Listeners for File Operations:
// app/Providers/EventServiceProvider.php
protected $listen = [
'joomla.filesystem.file.created' => [
\App\Listeners\LogFileUpload::class,
],
];
Trigger events manually:
event(new \Joomla\Filesystem\Event\FileCreated($path));
Override Default Behavior:
File or Folder) and bind them in Laravel’s service container:
$this->app->bind(\Joomla\Filesystem\File::class, function ()
How can I help you explore Laravel packages today?