yiisoft/files
Yii Files is a PHP 8+ utility package with FileHelper methods for common filesystem tasks: ensure/create directories with permissions, remove or clear directories, filter files via path matching, and other file and directory management helpers.
Installation:
composer require yiisoft/files
No additional configuration is required for basic usage.
First Use Case: Create a file or directory in a Laravel project:
use Yiisoft\Files\FileHelper;
// Create a directory
FileHelper::createDirectory('storage/app/uploads');
// Write to a file
FileHelper::writeFile('storage/app/uploads/test.txt', 'Hello, Yii!');
Key Classes:
Yiisoft\Files\FileHelper: Core utility class for file/directory operations.Yiisoft\Files\PathHelper: Path manipulation utilities.Yiisoft\Files\Filesystem: Filesystem abstraction (if extended).Where to Look First:
File Operations:
// Read/write files
$content = FileHelper::readFile('path/to/file.txt');
FileHelper::writeFile('path/to/file.txt', $content, FileHelper::WRITE_APPEND);
// Handle file existence
if (FileHelper::isFile('path/to/file.txt')) {
FileHelper::deleteFile('path/to/file.txt');
}
Directory Management:
// Create directories recursively
FileHelper::createDirectory('storage/app/cache', 0755, true);
// List files/directories
$files = FileHelper::findFiles('storage/app/uploads', ['*.jpg']);
$dirs = FileHelper::findDirectories('storage/app/uploads');
Path Manipulation:
// Normalize paths (cross-platform)
$normalized = PathHelper::normalize('path/with/../duplicates');
// Join paths safely
$fullPath = PathHelper::concat('storage', 'app', 'uploads');
Integration with Laravel:
storage_path() or public_path() helpers to resolve Laravel-specific paths:
$laravelPath = storage_path('app/uploads');
FileHelper::createDirectory($laravelPath);
File facade for hybrid usage:
use Illuminate\Support\Facades\File as LaravelFile;
// YiiSoft for advanced operations, Laravel for simplicity
LaravelFile::exists($path) ? FileHelper::deleteFile($path) : null;
Batch Processing:
// Process all files in a directory
foreach (FileHelper::findFiles('storage/app/logs') as $file) {
if (FileHelper::getFileExtension($file) === 'log') {
// Process log file
}
}
Temporary Files:
$tempDir = FileHelper::createTempDirectory('prefix_');
FileHelper::writeFile($tempDir . '/temp.txt', 'Temporary data');
// Cleanup later: FileHelper::deleteDirectory($tempDir);
Path Separators:
PathHelper::normalize() or PathHelper::concat() to avoid cross-platform issues (e.g., path/to/file vs. path\to\file)./ or \ in paths will break on Windows/Linux.Permissions:
FileHelper::createDirectory() requires writable parent directories. Use 0755 or 0777 for explicit permissions, but avoid 0777 in production.FileHelper::isWritable('path').File Locking:
flock() or Laravel’s Storage facade for concurrent access:
$file = storage_path('file.lock');
$handle = fopen($file, 'w');
flock($handle, LOCK_EX); // Acquire lock
// Critical section
flock($handle, LOCK_UN); // Release lock
fclose($handle);
Recursive Operations:
FileHelper::deleteDirectory() will not prompt for confirmation. Use cautiously in production:
FileHelper::deleteDirectory('storage/app/cache', true); // Force delete
FileHelper::isDirectoryEmpty() first.Symbolic Links:
FileHelper::isFile() and FileHelper::isDirectory() follow symlinks by default. Use FileHelper::isFileReal() or FileHelper::isDirectoryReal() to check the target.Memory Usage:
FileHelper::readFile() with caution for files >100MB. Stream content instead:
$stream = fopen('large_file.zip', 'r');
while (!feof($stream)) {
// Process chunk by chunk
}
fclose($stream);
dd(FileHelper::normalize('path/with/../issues')); // Debug normalized path
if (!FileHelper::isFile($path)) {
throw new \RuntimeException("File not found: {$path}");
}
\Log::debug('Creating directory', ['path' => $path, 'permissions' => 0755]);
FileHelper::createDirectory($path, 0755);
Custom Filesystem:
Yiisoft\Files\Filesystem to integrate with Laravel’s FilesystemManager:
use Yiisoft\Files\Filesystem;
class LaravelFilesystem extends Filesystem {
public function url($path) {
return asset('storage/' . $path);
}
}
config/filesystems.php:
'disks' => [
'yiisoft' => [
'driver' => 'custom',
'class' => LaravelFilesystem::class,
],
],
Event Hooks:
filesystem events (requires additional setup):
use Illuminate\Filesystem\Events\FileCreated;
event(new FileCreated($path));
Path Aliases:
PathHelper extension for Laravel’s app_path(), config_path(), etc.:
PathHelper::addAlias('app', app_path());
$appPath = PathHelper::concat('app', 'Http', 'Controllers');
Testing:
Storage facade to mock filesystem operations in tests:
Storage::fake('yiisoft');
FileHelper::writeFile('test.txt', 'data');
Storage::disk('yiisoft')->assertExists('test.txt');
How can I help you explore Laravel packages today?