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)3116.3k↓11.9%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 1mo 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 82% 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

970d 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://www.gravatar.com/avatar/bbd2c909c6fe4f925dc392240dccd12990b7b545a22af91ca6a4c22e9459f2d4?d=identicon)[cs278](/maintainers/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

[laravel-admin-ext/china-distpicker

Distpicker extension for laravel-admin

13443.9k](/packages/laravel-admin-ext-china-distpicker)[godbout/dash-docset-builder

Dash (LOVE) Docset Builder in PHP (LOVE).

1253.5k](/packages/godbout-dash-docset-builder)[jiannei/laravel-enum

A simple and easy-to-use enumeration extension package to help you manage enumerations in your project more conveniently, supporting Laravel and Lumen

3933.4k5](/packages/jiannei-laravel-enum)[jimbojsb/workman

PHP process forking &amp; daemonizing library

608.8k](/packages/jimbojsb-workman)[droptica/droopler-project

Project template for Droopler with Composer

5010.0k](/packages/droptica-droopler-project)[mdmsoft/yii2-widgets

Widgets for Yii2

178.1k](/packages/mdmsoft-yii2-widgets)

PHPackages © 2026

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