neutron/temporary-filesystem
TemporaryFilesystem provides a simple PHP API to create temporary directories and files using Symfony’s Filesystem component. Generate single or multiple empty temp files with custom prefixes, suffixes, and extensions—handy for concurrent processes and libraries relying on file extensions.
Installation:
composer require neutron/temporary-filesystem
No additional configuration is required—just autoload the package.
First Usage:
use Neutron\TemporaryFilesystem\TemporaryFilesystem;
$fs = TemporaryFilesystem::create();
This initializes a temporary filesystem backed by Symfony’s Filesystem component.
First Use Case: Create a temporary directory for processing uploads or generating assets:
$tempDir = $fs->createTemporaryDirectory();
// Use $tempDir for operations (e.g., `Storage::put($tempDir.'/file.txt', ...)`)
// Filesystem auto-cleans up on script exit (or manually via `$fs->delete()`).
Temporary File Handling:
$tempFile = $fs->createTemporaryFile('prefix_', '.suffix', 'ext');
// Example: $tempFile = '/tmp/tmp_abc123_prefix_.ext'
$tempFiles = $fs->createTemporaryFiles(10, 'thumb_', '.jpg');
// Useful for batch processing (e.g., image thumbnails).
Directory-Based Operations:
$dir = $fs->createTemporaryDirectory(0755);
try {
// Perform operations in $dir (e.g., `File::put($dir.'/file.txt', ...)`).
} finally {
$fs->delete($dir); // Force cleanup (optional).
}
Storage facade or Symfony\Component\Filesystem\Filesystem for recursive operations.Integration with Laravel:
use Illuminate\Support\Facades\Storage;
$tempPath = $fs->createTemporaryFile();
Storage::put($tempPath, file_get_contents('original_file'));
Cleanup Strategies:
$fs instance is garbage-collected.$fs->delete($path) for explicit cleanup (e.g., in finally blocks).Custom Temporary Path: Override the default temp directory (e.g., for testing):
$fs = TemporaryFilesystem::create(sys_get_temp_dir().'/custom_temp');
Context Managers: Encapsulate usage in a trait or helper:
trait UsesTempFiles {
protected function withTempFile(callable $callback) {
$fs = TemporaryFilesystem::create();
$file = $fs->createTemporaryFile();
try {
return $callback($file);
} finally {
$fs->delete($file);
}
}
}
Symfony Filesystem Integration:
Extend functionality using Symfony’s Filesystem methods:
$fs->mirror($sourceDir, $fs->createTemporaryDirectory());
Memory Leaks:
$fs and ensure cleanup in destroy() or finally blocks.Permissions:
0755) or verify sys_get_temp_dir() permissions.Path Portability:
/tmp/ on Linux).realpath() if sharing across environments.Race Conditions:
uniqid().'_prefix_').if (!$fs->exists($path)) {
// Debug: Check if file was deleted.
}
$fs->getTempDir(); // Returns the root temp directory.
Custom Naming: Override the random string generator:
$fs->setRandomStringGenerator(fn() => uniqid());
Event Hooks: Extend the class to add pre/post-cleanup hooks:
class HookedTemporaryFilesystem extends TemporaryFilesystem {
public function delete($path) {
Log::debug("Deleting: $path");
parent::delete($path);
}
}
Storage Backend: For non-disk storage (e.g., S3), wrap the filesystem:
$fs = new TemporaryFilesystem(new S3FilesystemAdapter());
createTemporaryFiles() over looping createTemporaryFile().$fs instance for multiple operations in a request/job.$fs = TemporaryFilesystem::create(storage_path('tests_temp'));
// Useful for isolated test environments.
handle() or use a finally block in the job class.How can I help you explore Laravel packages today?