PHPackages                             spatie/temporary-directory - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. spatie/temporary-directory

ActiveLibrary[Testing &amp; Quality](/categories/testing)

spatie/temporary-directory
==========================

Easily create, use and destroy temporary directories

2.3.1(4mo ago)96789.9M—2.3%47[1 PRs](https://github.com/spatie/temporary-directory/pulls)20MITPHPPHP ^8.0CI passing

Since Jan 31Pushed 1mo ago4 watchersCompare

[ Source](https://github.com/spatie/temporary-directory)[ Packagist](https://packagist.org/packages/spatie/temporary-directory)[ Docs](https://github.com/spatie/temporary-directory)[ Fund](https://spatie.be/open-source/support-us)[ GitHub Sponsors](https://github.com/spatie)[ RSS](/packages/spatie-temporary-directory/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (1)Versions (28)Used By (20)

Quickly create, use and delete temporary directories
====================================================

[](#quickly-create-use-and-delete-temporary-directories)

[![Latest Version on Packagist](https://camo.githubusercontent.com/e6ea213c6b046b0f8bc33640828e3d38e0ac17ec14ef51eaf5fd61598c2f6908/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f74656d706f726172792d6469726563746f72792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/temporary-directory)[![Tests](https://github.com/spatie/temporary-directory/workflows/run-tests/badge.svg?label=tests)](https://github.com/spatie/temporary-directory/workflows/run-tests/badge.svg?label=tests)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE.md)[![Total Downloads](https://camo.githubusercontent.com/0e1179ba64522e22b92e8d085959ad5e26ddecf23a3f7545809a9ee3f8a5e732/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f74656d706f726172792d6469726563746f72792e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/temporary-directory)

This package allows you to quickly create, use and delete a temporary directory in the system's temporary directory.

Here's a quick example on how to create a temporary directory and delete it:

```
use Spatie\TemporaryDirectory\TemporaryDirectory;

$temporaryDirectory = (new TemporaryDirectory())->create();

// Get a path inside the temporary directory
$temporaryDirectory->path('temporaryfile.txt');

// Delete the temporary directory and all the files inside it
$temporaryDirectory->delete();
```

Support us
----------

[](#support-us)

[![](https://camo.githubusercontent.com/e3d2d23caced9e599279993a0b5ccc304ac5045048d4209ba4533bd5c94cde1d/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f74656d706f726172792d6469726563746f72792e6a70673f743d31)](https://spatie.be/github-ad-click/temporary-directory)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require spatie/temporary-directory
```

Usage
-----

[](#usage)

### Creating a temporary directory

[](#creating-a-temporary-directory)

To create a temporary directory simply call the `create` method on a `TemporaryDirectory` object.

```
(new TemporaryDirectory())->create();
```

Alternatively, use the static `make` method on a `TemporaryDirectory` object.

```
TemporaryDirectory::make();
```

By default, the temporary directory will be created in a timestamped directory in your system's temporary directory (usually `/tmp`).

### Naming your temporary directory

[](#naming-your-temporary-directory)

If you want to use a custom name for your temporary directory instead of the timestamp call the `name` method with a string `$name` argument before the `create` method.

```
(new TemporaryDirectory())
   ->name($name)
   ->create();
```

By default an exception will be thrown if a directory already exists with the given argument. You can override this behaviour by calling the `force` method in combination with the `name` method.

```
(new TemporaryDirectory())
   ->name($name)
   ->force()
   ->create();
```

### Setting a custom location for a temporary directory

[](#setting-a-custom-location-for-a-temporary-directory)

You can set a custom location in which your temporary directory will be created by passing a string `$location` argument to the `TemporaryDirectory` constructor.

```
(new TemporaryDirectory($location))
   ->create();
```

The `make` method also accepts a `$location` argument.

```
TemporaryDirectory::make($location);
```

Finally, you can call the `location` method with a `$location` argument.

```
(new TemporaryDirectory())
   ->location($location)
   ->create();
```

### Setting custom permissions for a temporary directory

[](#setting-custom-permissions-for-a-temporary-directory)

By default, the temporary directory will be created with permissions `0777`. You can customize these permissions by calling the `permission` method with an integer `$permission` argument before the `create` method.

```
(new TemporaryDirectory())
   ->permission(0755)
   ->create();
```

### Determining paths within the temporary directory

[](#determining-paths-within-the-temporary-directory)

You can use the `path` method to determine the full path to a file or directory in the temporary directory:

```
$temporaryDirectory = (new TemporaryDirectory())->create();
$temporaryDirectory->path('dumps/datadump.dat'); // return  /tmp/1485941876276/dumps/datadump.dat
```

### Emptying a temporary directory

[](#emptying-a-temporary-directory)

Use the `empty` method to delete all the files inside the temporary directory.

```
$temporaryDirectory->empty();
```

### Deleting a temporary directory

[](#deleting-a-temporary-directory)

Once you're done processing your temporary data you can delete the entire temporary directory using the `delete` method. All files inside of it will be deleted.

```
$temporaryDirectory->delete();
```

### Deleting a temporary directory when the object is destroyed

[](#deleting-a-temporary-directory-when-the-object-is-destroyed)

If you want to automatically have the filesystem directory deleted when the object instance has no more references in its defined scope, you can enable `deleteWhenDestroyed()` on the TemporaryDirectory object.

```
function handleTemporaryFiles()
{
    $temporaryDirectory = (new TemporaryDirectory())
        ->deleteWhenDestroyed()
        ->create();

    // ... use the temporary directory

    return; // no need to manually call $temporaryDirectory->delete()!
}

handleTemporaryFiles();
```

You can also call `unset()` on an object instance.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Postcardware
------------

[](#postcardware)

You're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.

Our address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.

We publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).

Credits
-------

[](#credits)

- [Alex Vanderbist](https://github.com/AlexVanderbist)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

74

—

ExcellentBetter than 100% of packages

Maintenance84

Actively maintained with recent releases

Popularity75

Solid adoption and visibility

Community47

Growing community involvement

Maturity78

Established project with proven stability

 Bus Factor2

2 contributors hold 50%+ of commits

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~142 days

Recently: every ~247 days

Total

24

Last Release

126d ago

Major Versions

0.0.3 → 1.0.02017-02-01

1.3.0 → 2.0.02021-03-30

PHP version history (4 changes)0.0.1PHP ^7.0

1.2.0PHP ^7.2

1.3.0PHP ^7.2|^8.0

2.0.0PHP ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![freekmurze](https://avatars.githubusercontent.com/u/483853?v=4)](https://github.com/freekmurze "freekmurze (85 commits)")[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (37 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (17 commits)")[![Nielsvanpach](https://avatars.githubusercontent.com/u/10651054?v=4)](https://github.com/Nielsvanpach "Nielsvanpach (13 commits)")[![github-actions[bot]](https://avatars.githubusercontent.com/in/15368?v=4)](https://github.com/github-actions[bot] "github-actions[bot] (10 commits)")[![AdrianMrn](https://avatars.githubusercontent.com/u/12762044?v=4)](https://github.com/AdrianMrn "AdrianMrn (9 commits)")[![patinthehat](https://avatars.githubusercontent.com/u/5508707?v=4)](https://github.com/patinthehat "patinthehat (5 commits)")[![chapeupreto](https://avatars.githubusercontent.com/u/834048?v=4)](https://github.com/chapeupreto "chapeupreto (4 commits)")[![peter279k](https://avatars.githubusercontent.com/u/9021747?v=4)](https://github.com/peter279k "peter279k (3 commits)")[![sebastiandedeyne](https://avatars.githubusercontent.com/u/1561079?v=4)](https://github.com/sebastiandedeyne "sebastiandedeyne (3 commits)")[![erikn69](https://avatars.githubusercontent.com/u/4933954?v=4)](https://github.com/erikn69 "erikn69 (2 commits)")[![Gummibeer](https://avatars.githubusercontent.com/u/6187884?v=4)](https://github.com/Gummibeer "Gummibeer (2 commits)")[![akoepcke](https://avatars.githubusercontent.com/u/5311185?v=4)](https://github.com/akoepcke "akoepcke (2 commits)")[![Admiral-Enigma](https://avatars.githubusercontent.com/u/13486531?v=4)](https://github.com/Admiral-Enigma "Admiral-Enigma (2 commits)")[![cosmastech](https://avatars.githubusercontent.com/u/42181698?v=4)](https://github.com/cosmastech "cosmastech (2 commits)")[![riasvdv](https://avatars.githubusercontent.com/u/3626559?v=4)](https://github.com/riasvdv "riasvdv (2 commits)")[![willrowe](https://avatars.githubusercontent.com/u/7152423?v=4)](https://github.com/willrowe "willrowe (1 commits)")[![AhmadWaleed](https://avatars.githubusercontent.com/u/23218299?v=4)](https://github.com/AhmadWaleed "AhmadWaleed (1 commits)")[![angeljqv](https://avatars.githubusercontent.com/u/79208641?v=4)](https://github.com/angeljqv "angeljqv (1 commits)")[![Chris8934](https://avatars.githubusercontent.com/u/44963939?v=4)](https://github.com/Chris8934 "Chris8934 (1 commits)")

---

Tags

directoryphpstoragetestingphpspatietemporary-directory

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/spatie-temporary-directory/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-temporary-directory/health.svg)](https://phpackages.com/packages/spatie-temporary-directory)
```

###  Alternatives

[vimeo/psalm

A static analysis tool for finding errors in PHP applications

5.8k77.5M6.7k](/packages/vimeo-psalm)[larastan/larastan

Larastan - Discover bugs in your code without running it. A phpstan/phpstan extension for Laravel

6.4k43.5M5.2k](/packages/larastan-larastan)[dave-liddament/sarb

Provides tools for baselining static analysis results and comparing against that baseline

1651.4M](/packages/dave-liddament-sarb)[letsdrink/ouzo-goodies

Utility classes, test assertions and mocking framework extracted from Ouzo framework.

132617.9k7](/packages/letsdrink-ouzo-goodies)[spatie/pest-expectations

A collection of handy custom Pest customisations

78107.2k12](/packages/spatie-pest-expectations)[quizlet/hammock

Hammock is a stand-alone mocking library for Hacklang.

27445.5k](/packages/quizlet-hammock)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
