Page MenuHomePhabricator

final class TempFile
libphutil Technical Documentation (Filesystem)

Simple wrapper to create a temporary file and guarantee it will be deleted on object destruction. Used like a string to path:

$temp = new TempFile();
Filesystem::writeFile($temp, 'Hello World');
echo "Wrote data to path: ".$temp;

Throws Filesystem exceptions for errors.

Tasks

Creating a Temporary File

  • public function __construct($filename, $root_directory) — Create a new temporary file.

Configuration

  • public function setPreserveFile($preserve) — Normally, the file is deleted when this object passes out of scope. You can set it to be preserved instead.

Internals

  • public function __toString() — Get the path to the temporary file. Normally you can just use the object in a string context.
  • public function __destruct() — When the object is destroyed, it destroys the temporary file. You can change this behavior with @{method:setPreserveFile}.

Methods

public function __construct($filename, $root_directory)

Create a new temporary file.

Parameters
string?$filenameFilename hint. This is useful if you intend to edit the file with an interactive editor, so the user's editor shows "commit-message" instead of "p3810hf-1z9b89bas".
string?$root_directoryRoot directory to hold the file. If omitted, the system temporary directory (often "/tmp") will be used by default.
Return
this//Implicit.//

public function setPreserveFile($preserve)

Normally, the file is deleted when this object passes out of scope. You can set it to be preserved instead.

Parameters
bool$preserveTrue to preserve the file after object destruction.
Return
this

public function __toString()

Get the path to the temporary file. Normally you can just use the object in a string context.

Return
stringAbsolute path to the temporary file.

public function __destruct()

When the object is destroyed, it destroys the temporary file. You can change this behavior with setPreserveFile().

Return
wild