4,000 emails/month for free | Mailtrap sends real emails now!

Temporary Directory

spatie/temporary-directory image

Temporary Directory stats

Downloads
36.7M
Stars
800
Open Issues
0
Forks
43

View on GitHub →

Easily create, use and destroy temporary directories

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.

spatie photo

We create open source, digital products and courses for the developer community

Cube

Laravel Newsletter

Join 40k+ other developers and never miss out on new tips, tutorials, and more.


Spatie Temporary Directory Related Articles

Easily Manage Temporary Files in Laravel image

Easily Manage Temporary Files in Laravel

Read article
Shift logo

Shift

Running an old Laravel version? Instant, automated Laravel upgrades and code modernization to keep your applications fresh.

Shift
SaaSykit: Laravel SaaS Starter Kit logo

SaaSykit: Laravel SaaS Starter Kit

SaaSykit is a Multi-tenant Laravel SaaS Starter Kit that comes with all features required to run a modern SaaS. Payments, Beautiful Checkout, Admin Panel, User dashboard, Auth, Ready Components, Stats, Blog, Docs and more.

SaaSykit: Laravel SaaS Starter Kit
Lucky Media logo

Lucky Media

Get Lucky Now - the ideal choice for Laravel Development, with over a decade of experience!

Lucky Media
Get expert guidance in a few days with a Laravel code review logo

Get expert guidance in a few days with a Laravel code review

Expert code review! Get clear, practical feedback from two Laravel devs with 10+ years of experience helping teams build better apps.

Get expert guidance in a few days with a Laravel code review
Bacancy logo

Bacancy

Supercharge your project with a seasoned Laravel developer with 4-6 years of experience for just $3200/month. Get 160 hours of dedicated expertise & a risk-free 15-day trial. Schedule a call now!

Bacancy
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum