PHP basename() function

Definition

To get the filename part of a path, you can use the basename() function.

Syntax

PHP basename() function has the following syntax.

basename(filePath,extension)

Parameter

PHP basename() function takes a path as its first parameter and, optionally, an extension as its second parameter.

Return

The return value is the name of the file without the directory information.

If the filename has the same extension as specified, the extension is taken off also.

Example

Get the filename part of a path


<?PHP//ww  w .  j a  v  a  2s .c om
      $filename = basename("/home/somefile.txt");
      echo $filename . "\n";
      $filename = basename("/home/somefile.txt", ".php");
      echo $filename . "\n";
      $filename = basename("/home/somefile.txt", ".txt");
      echo $filename . "\n";
?>

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