Oihana PHP

getTemporaryDirectory.php

Table of Contents

Functions

getTemporaryDirectory()  : string
Builds a path inside the system temporary directory.

Functions

getTemporaryDirectory()

Builds a path inside the system temporary directory.

getTemporaryDirectory([string|array<string|int, string>|null $path = null ][, bool $assertable = false ][, bool $isReadable = true ][, bool $isWritable = false ]) : string
Parameters
$path : string|array<string|int, string>|null = null

Optional sub‑path(s) to append inside sys_get_temp_dir().

$assertable : bool = false

Whether to validate the final directory path. Defaults to false because the directory may not exist yet.

$isReadable : bool = true

Check if the directory is readable (Default true).

$isWritable : bool = false

Check if the directory is writable (Default false).

Tags
throws
DirectoryException

If validation is enabled and the path is invalid.

example

Basic usage: get the system temp directory.

use function oihana\files\getTemporaryDirectory;

echo getTemporaryDirectory();
// e.g. "/tmp" on Unix, "C:\Windows\Temp" on Windows

Append a subdirectory path:

echo getTemporaryDirectory('myapp/cache'); // e.g. "/tmp/myapp/cache"
echo getTemporaryDirectory(['myapp', 'logs']); // e.g. "/tmp/myapp/logs"

Validate that the directory exists and is readable:

try
{
    $dir = getTemporaryDirectory('myapp/logs', true); // assertable = true
    echo $dir;
}
catch ( DirectoryException $e )
{
    // Handle error if directory does not exist or is not readable
}

Validate that the directory is writable:

try
{
   // assertable + readable + writable
   $dir = getTemporaryDirectory('myapp/uploads', true, true, true);
   echo $dir;
}
catch (DirectoryException $e)
{
   // Handle permission error
}

Using an absolute path (bypasses sys_get_temp_dir):

echo getTemporaryDirectory('/var/tmp/myapp'); // stays as is on Unix
echo getTemporaryDirectory('C:\\Temp\\custom'); // stays as is on Windows

Edge case: skip path argument to return system temp dir directly:

echo getTemporaryDirectory(null); // same as sys_get_temp_dir()
echo getTemporaryDirectory('');   // same as sys_get_temp_dir()
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

Normalised temporary directory path.


        
On this page

Search results