Blog de programación, errores, soluciones

Chose Language:
Author: Admin/Publisher |finished | checked

PHP dirname function

The PHP dirname function allows us to obtain the path of parent directories based on the specified path and level.

Description / Descripción
dirname(string $path, int $levels = 1): string
Sintaxis / Sintax
dirname($path,$levels);

Note: The dirname() function operates naively on the input string and is not aware of the actual filesystem or path components like ".."

Caution:
On Windows, dirname() assumes the currently set codepage, so in order to see the correct directory name with multibyte character paths, the matching codepage must be set. If the path contains characters that are invalid for the current codepage, the behavior of dirname() is undefined.

On other systems, dirname() assumes that the path is encoded in an ASCII compatible encoding. Otherwise, the behavior of the function is undefined.

Parameters

$path – the path

En Windows , / y \ son usados como separadores de directorios(carpetas). En otros entornos como puede ser el de Linux , se utiliza /.

$levels -The number of parent directories to go back. It must be an integer greater than 0.

Returns

Returns a string that is the path of the parent directory. If there is no / in the path, a . is returned indicating the current directory. Additionally, the returned string is a string with a trailing / which is removed.

Ejemplos

Ejemplo simple de dirname()
<?php
echo dirname("project/assets/css/tipography.css") . PHP_EOL;
project/assets/css
Ejemplo simple de dirname() con levels
<?php
echo dirname("project/assets/css/tipography.css",2) . PHP_EOL;
project/assets
Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category