Easily create, use and destroy temporary directories
spatie/temporary-directory stats
- Downloads
- 36.7M
- Stars
- 800
- Open Issues
- 0
- Forks
- 43
Summary of Spatie's TemporaryDirectory Laravel Package
The spatie/temporary-directory
package provides a simple and efficient way to create, manage, and dispose of temporary directories within a Laravel application. This utility is particularly useful when handling temporary files during data processing, ensuring a clean and isolated environment that automatically cleans up after operations.
Key Features
- Easy Creation and Deletion: Quickly create and delete temporary directories.
- Custom Naming and Location: Set custom names and specify the location for temporary directories.
- Automatic Cleanup: Option to automatically delete the directory when the object is destroyed.
- Path Management: Conveniently determine paths within the temporary directory.
Installation
Install the package via Composer:
composer require spatie/temporary-directory
Usage Examples
Basic Temporary Directory Creation
use Spatie\TemporaryDirectory\TemporaryDirectory; $temporaryDirectory = (new TemporaryDirectory())->create();
Custom Directory Naming
$temporaryDirectory = (new TemporaryDirectory()) ->name('my-temp-dir') ->create();
Custom Location and Force Creation
$temporaryDirectory = (new TemporaryDirectory('path/to/location')) ->name('my-temp-dir') ->force() ->create();
Automatic Cleanup
function handleTemporaryFiles() { $temporaryDirectory = (new TemporaryDirectory()) ->deleteWhenDestroyed() ->create();}
Path Determination Within Temporary Directory
$path = $temporaryDirectory->path('subfolder/file.txt');
Deleting the Temporary Directory
$temporaryDirectory->delete();
Additional Information
- Testing: Run
composer test
to execute tests. - Contributions: Contributions are welcome via pull requests and issues.
- License: The package is open-sourced under the MIT license.
This package not only simplifies file handling tasks but also encourages clean coding practices by managing temporary data efficiently. Whether you're handling user uploads, generating temporary data during tests, or processing large datasets, spatie/temporary-directory
offers a robust solution to manage temporary files in Laravel applications.