WireTempDir

Manages the creation, tracking, and automatic cleanup of temporary directories under ProcessWire's cache path

Each instance creates a uniquely-named directory that is automatically removed when the object is destroyed (or at the next maintenance cycle if setRemove(false) was called). It is used throughout the core for short-lived scratch space — image resizing, file extraction, module downloads, and similar operations.

$td = new WireTempDir();
$tempDir = $td->get();              // returns path like /site/assets/cache/WireTempDir/.SomeName/0/
$td->setRemove(false);             // opt out of auto-removal
// ...write files into $tempDir...
$td->remove();                      // manually clean up

You can also instantiate with a name (for shared/access-by-name temp directories):

$td = wire(new WireTempDir($user));  // name = 'User'
$path = $td->get();                  // path under .User/
// directory persists across requests (with the same $name) until it expires

Expand all      API reference

Directory layout
/site/assets/cache/WireTempDir/         ← class root (shared by all instances)
  .User/                                ← name root (all temp dirs for $name = 'User')
    0/                                  ← individual temp dir (numeric or custom $id)
      .wtd                              ← hidden marker file (contains timestamp)
    1/
  .MyRandomNameRabc123/                ← randomized name root (auto-generated)
    0/

The .wtd hidden file is written into every directory created by WireTempDir; it serves as a marker so the class can distinguish its own directories from any manually created ones.

Properties
PropertyTypeDescription
tempDirMaxAgeintMaximum age in seconds for files in a named temp dir before they are considered expired (default 120).
cleanMaxAgeintAge in seconds after which directories are assumed abandoned and cleaned up during maintenance (default 86400 / 24h).
autoRemoveboolWhether the temp directory should be automatically removed in __destruct() (default true). Set with setRemove().
Constants
ConstantTypeDescription
hiddenFileNamestringName of the marker file placed in every created temp directory (value: .wtd).
Methods

init($name, $basePath)

Initialize the temp-directory root for this instance. Should be called only once. If a $name was provided to the constructor, init() runs automatically.

$td = new WireTempDir();
$td->init('MyFeature');
$root = $td->get();

Passing an object as $name uses the class name (minus namespace).

$td->init($page);    // name = 'Page'
  • $basePath – Optional override base path; must be within ProcessWire assets and be writable. Defaults to $config->paths->cache.
  • Returns the temp-root path string.
  • Throws WireException if the directory has already been initialized.

get($id)

Returns a usable temporary directory path, creating it if necessary. Subsequent calls within the same request return the same path (cached).

$path = $td->get();         // autogenerate numeric id
$files->filePutContents($path . 'test.txt', 'hello');

Using a custom identifier:

$path = $td->get('backup'); // path under .MyName/backup/
  • If $id collides with an existing directory, a numeric suffix is appended automatically.
  • Cascading retries: if mkdir() fails, the method retries (up to 5 levels) with a modified name; a WireException is thrown after that.

setMaxAge($tempDirMaxAge, $cleanMaxAge)

Update the maximum-age thresholds (in seconds). Chainable.

$td->setMaxAge(300, 86400);   // tempDirMaxAge = 5 min, cleanMaxAge = 1 day

setRemove($remove)

When false is passed, the temp directory will not be auto-removed when the object is destroyed; you take responsibility for calling remove() yourself. Chainable.

$td->setRemove(false);
// ...use the directory...
$td->remove();

remove()

Removes the temporary directory created by this instance, plus its parent name-root if unique to this instance. Also performs one-time maintenance cleanup of expired directories under the class root. Returns bool indicating success.

$ok = $td->remove();

removeAll()

Clears all temporary directories created by WireTempDir on this site (the entire class-root tree). Use with caution — this is destructive and affects other instances/feature areas.

$td->removeAll();

maintenance()

Static one-time cleanup pass that removes abandoned directories under the class root (default 24h threshold). This is invoked automatically the first time any temp directory is created or removed during a request, but you can call it manually as well.

$td->maintenance();

createName($prefix)

Generates a random name for a runtime/scoped temp dir. Exposed publicly so you can preview the name or designate a custom prefix.

$name = $td->createName('bw'); // e.g. 'bwT16903567870Rxyz…'

__toString()

Returns the result of get(), making it convenient to string-cast the object directly.

$td = new WireTempDir();
$path = (string) $td;
// or just:
$file = $td . '/test.txt';
Notes
  • Instantiation: new WireTempDir() or wire(new WireTempDir($objectOrName)). The constructor accepts a deprecated $name + $basePath argument list; prefer calling init() separately instead.
  • Auto-removal: By default the temp directory is removed in __destruct(). Use setRemove(false) to keep it across multiple requests.
  • Shared vs. scoped directories: If the name is a fixed string (or derived from an object's class name), directories persist across requests until expired. If auto-generated via createName(), they are unique to the instance and therefore safe to only remove at destruction.
  • Security: rmdir() refuses to remove directories that are not under the class root. Created directories receive a .wtd marker file, but the removal safety check is based on the class-root path. init() replaces an out-of-tree $basePath with the default cache path.
  • Class root: by default, files live under $config->paths->cache . 'WireTempDir/'.
  • __toString() returns the current temp-directory path, equivalent to calling get().
  • Source file: wire/core/Tools/WireTempDir/WireTempDir.php
  • Extends Wire.

API reference: methods

Click any linked item for full usage details and examples. Hookable methods are indicated with the icon. In addition to those shown below, the WireTempDir class also inherits all the methods and properties of: Wire.

Show class?     Show args?       Only hookable?    

Common

NameReturnSummary 
WireTempDir::create()
string

@deprecated Use init() method instead

 
WireTempDir::createName()
string

Create a randomized name for runtime temp dir

 
WireTempDir::get()
string

Returns a temporary directory (path)

 
WireTempDir::init()
string

Initialize temporary directory

 
WireTempDir::maintenance()
bool

Perform maintenance by cleaning up old temporary directories

 
WireTempDir::remove()
bool

Removes the temporary directory created by this object

 
WireTempDir::removeAll()
None

Clear all temporary directories created by this class

 
WireTempDir::setRemove()
$this

Call this with 'false' to prevent temp dir from being removed automatically when object is destructed

 

Additional methods and properties

In addition to the methods and properties above, WireTempDir also inherits the methods and properties of these classes:

API reference based on ProcessWire core version 3.0.269