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óndirname(string $path, int $levels = 1): stringSintaxis / 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
/
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/cssEjemplo simple de dirname() con levels
<?php echo dirname("project/assets/css/tipography.css",2) . PHP_EOL;
project/assets