Installation:
composer require makasim/temp-file
Add to composer.json if not auto-loaded:
"autoload": {
"psr-4": {
"Makasim\\File\\": "vendor/makasim/temp-file/src"
}
}
Run composer dump-autoload.
First Use Case: Generate a temporary file with default settings:
use Makasim\File\TempFile;
$tempFile = TempFile::generate();
echo "Temporary file created at: " . $tempFile->getPathname();
Where to Look First:
TempFile.php for core methods (generate(), from(), persist()).SplFileInfo docs (since TempFile extends it).Generating Temporary Files:
// Default: random name in system temp dir
$file = TempFile::generate();
// Custom path/prefix
$file = TempFile::generate('/custom/path', 'prefix_');
Copying Persisted Files:
$persistedFile = TempFile::from('/path/to/existing/file.txt');
// File is now a TempFile but persists until `persist()` or script end.
File Operations:
// Write data
file_put_contents($file->getPathname(), 'Hello, TempFile!');
// Read data (SplFileInfo methods)
$content = file_get_contents($file->getPathname());
Preventing Auto-Deletion:
$file = TempFile::generate();
$file->persist(); // File survives script shutdown
Contextual Usage:
// In a Laravel service:
public function processUploadedFile(UploadedFile $uploadedFile) {
$tempFile = TempFile::from($uploadedFile->getPathname());
// Process $tempFile...
}
Laravel Filesystem:
Use with Storage::disk('local')->put() for hybrid temp/persisted storage:
$tempFile = TempFile::generate();
Storage::disk('local')->put($tempFile->getFilename(), $tempFile->getContent());
Testing:
Mock TempFile in unit tests to avoid filesystem I/O:
$mockFile = $this->createMock(SplFileInfo::class);
$this->app->instance(TempFile::class, $mockFile);
Cleanup: Manually delete files if needed (though auto-cleanup is default):
if ($file->exists()) {
$file->delete();
}
Auto-Deletion Timing:
unlink() or delete() unless persist() is called.Path Validation:
generate() uses sys_get_temp_dir() by default. Ensure the target directory is writable:
if (!is_writable(sys_get_temp_dir())) {
throw new RuntimeException('Temp directory not writable');
}
Race Conditions:
$file = TempFile::generate(null, 'unique_' . uniqid());
Laravel Artisan Commands:
Artisan::call() without proper shutdown).Windows Line Endings:
\r\n line endings, which can cause issues in cross-platform apps. Normalize if needed:
$content = str_replace(["\r\n", "\r"], "\n", $content);
Verify File Existence:
if (!$file->exists()) {
throw new RuntimeException("Temp file not created at: " . $file->getPathname());
}
Check Permissions:
if (!is_writable(dirname($file->getPathname()))) {
throw new RuntimeException("Cannot write to temp directory");
}
Custom Storage: Override the temp directory in a child class:
class CustomTempFile extends TempFile {
public static function generate($path = null, $prefix = null) {
return new static(sys_get_temp_dir() . '/custom/', $prefix);
}
}
Post-Shutdown Hooks: Register a shutdown function to log temp file cleanup:
register_shutdown_function(function() {
$tempFiles = TempFile::getAll(); // Hypothetical; not natively supported
error_log("Cleaned up " . count($tempFiles) . " temp files");
});
Laravel Service Provider: Bind the package to the container for dependency injection:
$this->app->bind(TempFile::class, function() {
return TempFile::generate();
});
generate($path, $prefix))./tmp on Linux).How can I help you explore Laravel packages today?