PHPackages                             cs278/mktemp - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. cs278/mktemp

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

cs278/mktemp
============

Create temporary files and directories using PHP safely.

v1.3.0(2y ago)4125.5k↓40.4%12MITPHPPHP ^5.3.3 || ^7 || ^8

Since Jan 24Pushed 2y ago1 watchersCompare

[ Source](https://github.com/cs278/php-mktemp)[ Packagist](https://packagist.org/packages/cs278/mktemp)[ RSS](/packages/cs278-mktemp/feed)WikiDiscussions master Synced 2d ago

READMEChangelog (3)Dependencies (2)Versions (5)Used By (2)

PHP mktemp
==========

[](#php-mktemp)

Create temporary files and directories in PHP, safely.

Sadly PHP is deficient when it comes to creating temporary files and directories in a safe and secure manner. This library aims to address these short-comings.

Usage
-----

[](#usage)

### Functions

[](#functions)

- `Cs278\Mktemp\temporaryFile(string|null $template, string|null $dir)`

    Create a temporary file with a supplied template for the name.

    The template should be a string containing a sequence of at *least* 3 consecutive `X` characters, these characters will be replaced with a random component. The last valid sequence in the string will be used. If this argument is `null` a default `tmp.XXXXXX` template will be used.

    The directory should be a path that exists and the user can write to, if this argument is not supplied the default temporary directory will be used.

    A `Cs278\Mktemp\Exception\CreateFailedException` will be thrown if a file cannot be created.

    ```
    use function Cs278\Mktemp\temporaryFile;

    var_dump(temporaryFile());
    // /tmp/tmp.P9aLnd
    var_dump(temporaryFile('output.XXXX.pdf'));
    // /tmp/output.oI7b.pdf
    var_dump(temporaryFile(null, '/var/tmp'));
    // /var/tmp/tmp.8uJx
    var_dump(temporaryFile('test.XXX.html', '/var/tmp'));
    // /var/tmp/test.9h2.html
    ```
- `Cs278\Mktemp\temporaryDir(string|null $template, string|null $dir)`

    Create a temporary directory with a supplied template for the name.

    The template should be a string containing a sequence of at *least* 3 consecutive `X` characters, these characters will be replaced with a random component. The last valid sequence in the string will be used. If this argument is `null` a default `tmp.XXXXXX` template will be used.

    The directory should be a path that exists and the user can write to, if this argument is not supplied the default temporary directory will be used.

    A `Cs278\Mktemp\Exception\CreateFailedException` will be thrown if a directory cannot be created.

    ```
    use function Cs278\Mktemp\temporaryDir;

    var_dump(temporaryDir());
    // /tmp/tmp.P9aLnd
    var_dump(temporaryDir('output.XXXX.pdf'));
    // /tmp/output.oI7b.pdf
    var_dump(temporaryDir(null, '/var/tmp'));
    // /var/tmp/tmp.8uJx
    var_dump(temporaryDir('test.XXX.html', '/var/tmp'));
    // /var/tmp/test.9h2.html
    ```

### `Cs278\Mktemp\TempFile`

[](#cs278mktemptempfile)

- `TempFile::__construct(string|null $template, string|null $dir)`

    Create a temporary file object that manages the life cycle of the file system entity.

    Arguments are the same as those documented for `temporaryFile()`.
- `TempFile::getPath()`

    Return the path to the temporary file, this is mainly intended to be used when interacting with the file outside of PHP.
- `TempFile::open(string $mode)`

    Open the file for reading/writing according to the supplied mode, defaults to `r+`. Returns an `\SplFileObject` that maybe used to manipulate the contents of the temporary file.
- `TempFile::keep(string $newPath)`

    Persist the temporary file into permanent file at the path specified, returns a new `\SplFileInfo` representing the new file.
- `TempFile::copyTo(resource $stream)`

    Copy the contents of the file into the supplied stream.
- `TempFile::release()`

    Removes the temporary file from disk.

###  Health Score

36

—

LowBetter than 79% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity36

Limited adoption so far

Community14

Small or concentrated contributor base

Maturity61

Established project with proven stability

 Bus Factor1

Top contributor holds 97.3% of commits — single point of failure

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 ~931 days

Total

4

Last Release

1016d ago

PHP version history (2 changes)v1.0.0PHP ^5.3.3 | ^7.0.0

v1.1.0PHP ^5.3.3 || ^7 || ^8

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/17377?v=4)[Chris Smith](/maintainers/cs278)[@cs278](https://github.com/cs278)

---

Top Contributors

[![cs278](https://avatars.githubusercontent.com/u/17377?v=4)](https://github.com/cs278 "cs278 (36 commits)")[![kingmaker-agm](https://avatars.githubusercontent.com/u/23194965?v=4)](https://github.com/kingmaker-agm "kingmaker-agm (1 commits)")

### Embed Badge

![Health badge](/badges/cs278-mktemp/health.svg)

```
[![Health](https://phpackages.com/badges/cs278-mktemp/health.svg)](https://phpackages.com/packages/cs278-mktemp)
```

###  Alternatives

[protonemedia/laravel-task-runner

Write Shell scripts like Blade Components and run them locally or on a remote server

1335.5k1](/packages/protonemedia-laravel-task-runner)

PHPackages © 2026

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