Oihana PHP

getDirectory.php

Table of Contents

Functions

getDirectory()  : string
Normalises (and optionally validates) a directory path.

Functions

getDirectory()

Normalises (and optionally validates) a directory path.

getDirectory(string|array<string|int, mixed>|null $path[, bool $assertable = true ][, bool $isReadable = true ][, bool $isWritable = false ]) : string
  • If $path is an array, empty segments and Char::EMPTY are removed, then the remaining parts are joined with DIRECTORY_SEPARATOR.
  • If $assertable is true (default), assertDirectory() ensures the resulting path exists and is readable.
  • Trailing separators are always stripped before return.
Parameters
$path : string|array<string|int, mixed>|null

Directory or segments to normalise.
Examples: '/tmp' or ['tmp','logs'].

$assertable : bool = true

Whether to validate the resulting path. Default: true.

$isReadable : bool = true

Whether to assert that the directory is readable. Default: true.

$isWritable : bool = false

Whether to assert that the directory is writable. Default: false.

Tags
throws
DirectoryException

If validation is enabled and the directory is invalid.

example

Basic use with a character string Validates that the system temporary directory exists and deletes the final separator.

$path = getDirectory( sys_get_temp_dir() . DIRECTORY_SEPARATOR );
// $path contient maintenant quelque chose comme '/tmp' ou 'C:\Users\...\Temp'.

Builds and validates a path from an array. Empty or null elements are ignored (assumes ‘/tmp/logs’ exists and is readable).

$parts = [sys_get_temp_dir(), '', 'logs', null];
$path = getDirectory($parts);
// $path contient maintenant quelque chose comme '/tmp/logs'.

Normalizes a path without validating it Ne lève pas d'exception si le chemin n'existe pas.

$path = getDirectory('/path/not/exist/', assertable: false);
// $path contains '/path/not/exist/'.

Validates that a directory is also writable.

try
{
    $path = getDirectory(sys_get_temp_dir(), isWritable: true);
    // The script continue if the directory is writable
}
catch ( DirectoryException $e )
{
    // Thrown an error
}
author

Marc Alcaraz (ekameleon)

since
1.0.0
Return values
string

Normalized directory path.


        
On this page

Search results