# RFC: Modify tempnam() to handle directories and auto-cleanup

- Status: Withdrawn
- Authors: Sara Golemon pollita@php.net
- Date: 2012-11-26
- Version: 1.0
- Original: https://wiki.php.net/rfc/request-tempnam
- Page: https://externals.io/rfc/request-tempnam
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Discussions on the mailing list

- [\[RFC\] Modify tempnam() to handle directories and auto-cleanup](https://externals.io/message/64054.md) — started on 2012-11-26, 5 messages

---

## Introduction

This RFC recommends a set of changes to the tempnam() standard function.

1. Extend the signature to include an “options” parameter (default keeping current behavior).
2. Define TEMPNAM\_DIR constant which, when passed to options, creates a directory instead of a file.
3. Define TEMPNAM\_REQUEST constant which, when passed to options, automatically removes the file/directory at the end of the request.

## TEMPNAM\_REQUEST

It's a common bug to forget to remove temporary files and after a period of time these may overpopulate the filesystem. TEMPNAM\_REQUEST seeks to avoid this by building a mechanism to automatically clean up after scripts.

```
$fn = tempnam("/tmp/", "test-", TEMPNAM_REQUEST);
file_put_contents($fn, "This is some data");
echo shell_exec("/usr/bin/someprog " . escapeshellarg($fn));
// No need to explicitly unlink($fn), since it will be cleaned up at request end
```

## TEMPNAM\_DIR

TEMPNAM\_DIR expands the scope and usability of this function to whole directories of files while keeping the number of entities which need to be tracked low.

```
$file = tempnam("/tmp/", "", TEMPNAM_REQUEST);
copy('http://www.php.net/get/php-5.4.9.tar.gz/from/us3.php.net/mirror', $file);

$dir  = tempnam("/tmp/", "", TEMPNAM_DIR | TEMPNAM_REQUEST);
$dir  = escapeshellarg($dir);
$file = escapeshellarg($file);
shell_exec("cd $dir && tar -zxf $file");
// tarball expanded into dir, both will be removed at request end
```

## tmpfile()

Although the existing function tmpfile() provides for the TEMPNAM\_REQUEST cleanup functionality, it does not provide an actual filename which can be passed to external programs.

## Implementation

- Add a HashTable to FG() locals for tracking request-temporary files/directories.
- Expand tempnam() to call existing system functions when options == 0
- For cases when options != 0, generate a filename using dir/prefix and uniqid() (or other mechanism) and attempt to create manually.
- For the TEMPNAM\_REQUEST case specifically, add the generated filename to FG(request\_tempnam) HashTable.
- At end of request, iterate through FG(request\_tempnam) removing all temporary files/dirs.

## Other Options

- Leave tempnam() as it is, but add request\_cleanup() function(s) to manage list of files to delete at request end. e.g. request\_cleanup($fn = tempnam(“/tmp”,“”));
- Add tempdir() function to separate that out from the request auto-cleanup subject
- ...

## Proposal and Patch

Coming soon, giving a chance to start discussion early.
