PHP pathinfo() Function

Definition

The pathinfo() function returns an array that contains information about a path.

Syntax

PHP pathinfo() Function has the following syntax.

pathinfo(path,options)

Parameter

ParameterIs RequiredDescription
pathRequired.Path to check
optionsOptional.Which array elements to return. Default is all

Possible values for options:

  • PATHINFO_DIRNAME - return only dirname
  • PATHINFO_BASENAME - return only basename
  • PATHINFO_EXTENSION - return only extension

Return

If the options parameter is not passed, an associative array containing the following elements is returned: dirname, basename, extension (if any), and filename.

Example

The pathinfo() function takes a filename as its only parameter and returns an array with three elements: dirname, basename, and extension.

Dirname contains the name of the directory the file is in, basename contains the base filename, and extension contains the file extension, if any (e.g., html or txt).

You can see this information yourself by running this script:


<?PHP//w  w  w  .j a  va 2 s  .c o  m
      $filename = "test.txt";
      $fileinfo = pathinfo($filename);
      var_dump($fileinfo);
      
      print_r(pathinfo("test.txt",PATHINFO_BASENAME));
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions