Blog de programación, errores, soluciones

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

PHP pathinfo function

The PHP pathinfo function in PHP returns information about the path of a file or directory in the form of an associative array or a string, depending on the option chosen in $option.

Description / Descripción
pathinfo(string $path, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME): mixed

Caution
The pathinfo() function takes into account the locale setting (regional config). Therefore, to correctly parse a path that contains multibyte characters, the corresponding locale setting must be set using the setlocale() function.

Parameters

$path – the path to be analyzed.

$options – You have these constants as possible parameters:

  • PATHINFO_DIRNAME,
  • PATHINFO_BASENAME,
  • PATHINFO_EXTENSION
  • PATHINFO_FILENAME
  • PATHINFO_ALL

Returns

If $options is not specified, the constant PATHINFO_ALL will be used by default. This will return all available elements as an associative array.

If another constant (flag) is used, it will return the specified element(s) as a string.

Aspects to consider:
If the path has more than one extension, PATHINFO_EXTENSION will only return the last one.
If the path has more than one extension, PATHINFO_FILENAME will only remove the last one.
If the path does not have an extension, no value will be returned.
If the basename starts with a dot, the following characters will be considered the extension and the filename will be empty.

Examples

Firstly, we need to know what elements are returned inside the associative array, which I couldn’t find on php.net. However, we can find out by running a var_dump on the result.

Knowing the associative array returned
$path = '/home/user/Documents/archivo.txt';
$info = pathinfo($path);
var_dump($info);
array(4) {
  ["dirname"]=>
  string(20) "/home/user/Documents"
  ["basename"]=>
  string(11) "archivo.txt"
  ["extension"]=>
  string(3) "txt"
  ["filename"]=>
  string(7) "archivo"
}

As you can see, info['basename'] gives the same result as running basename(), and info['dirname'] is equivalent to running dirname().

Remember that we saw that if our path starts with a dot, everything after it is treated as an extension. Let’s see an example of that now.

.<path>(probado en PHP 8.2.5)
$path = '/home/user/Documents/.archivo.txt';
$info = pathinfo($path);
var_dump($info);
array(4) {
  ["dirname"]=>
  string(20) "/home/user/Documents"
  ["basename"]=>
  string(12) ".archivo.txt"
  ["extension"]=>
  string(3) "txt"
  ["filename"]=>
  string(8) ".archivo"
}

I’m glad that this has pleasantly surprised you. It seems that in this case, it doesn’t behave exactly as mentioned in the notes on php.net. This could be due to version differences. However, it is true that it removes the last extension.

pathinfo en archivo sin extension
$partes_ruta = pathinfo('/path/emptyextension.');
var_dump($partes_ruta);
array(4) {
  ["dirname"]=>
  string(5) "/path"
  ["basename"]=>
  string(15) "emptyextension."
  ["extension"]=>
  string(0) ""
  ["filename"]=>
  string(14) "emptyextension"
}

Finally, I will provide an example for the constants that will give us a string as a return. Let’s remember that these constants are as follows:

  • PATHINFO_DIRNAME,
  • PATHINFO_BASENAME,
  • PATHINFO_EXTENSION
  • PATHINFO_FILENAME
retornos de strings usando constantes
$partes_ruta = pathinfo('/path/emptyextension/parr.txt',PATHINFO_DIRNAME);
var_dump($partes_ruta);
string(20) "/path/emptyextension"
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