The go-to PHP IDE with extensive out-of-the-box support for Laravel and its ecosystem.

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
CodeKudu logo

CodeKudu

Stand-ups, Retrospectives, and 360° Feedback for the entire team. 50% off with code LARAVELNEWS.

CodeKudu
Lucky Media logo

Lucky Media

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

Lucky Media
Kirschbaum logo

Kirschbaum

Providing innovation and stability to ensure your web application succeeds.

Kirschbaum
The Certification of Competence for Laravel logo

The Certification of Competence for Laravel

A community-driven, proctored assessment across 4 levels designed to validate real-world Laravel knowledge, from Junior to mastery-level Artisan. Official Vue.js, Official Nuxt, Angular, React, JS certifications also available.

The Certification of Competence for Laravel
Tinkerwell logo

Tinkerwell

The must-have code runner for Laravel developers. Tinker with AI, autocompletion and instant feedback on local and production environments.

Tinkerwell
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