Page MenuHomePhorge

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}.

Other Methods

Methods

public function __get($name)
Inherited

This method is not documented.
Parameters
$name
Return
wild

public function __set($name, $value)
Inherited

This method is not documented.
Parameters
$name
$value
Return
wild

public function current()
Inherited

This method is not documented.
Return
wild

public function key()
Inherited

This method is not documented.
Return
wild

public function next()
Inherited

This method is not documented.
Return
wild

public function rewind()
Inherited

This method is not documented.
Return
wild

public function valid()
Inherited

This method is not documented.
Return
wild

private function throwOnAttemptedIteration()
Inherited

This method is not documented.
Return
wild

public function getPhobjectClassConstant($key, $byte_limit)
Inherited

Phobject

Read the value of a class constant.

This is the same as just typing self::CONSTANTNAME, but throws a more useful message if the constant is not defined and allows the constant to be limited to a maximum length.

Parameters
string$keyName of the constant.
int|null$byte_limitMaximum number of bytes permitted in the value.
Return
stringValue of the constant.

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