PHP basename() Function | Get Filename from Path

The PHP basename() function is used to get the file name from specified path. For example:

<?php
   $path = "C:\Users\DEV\codescracker.com\myfile.txt";
   echo basename($path);
?>

The output of above PHP example, is:

php basename function

And here is the snapshot of the directory/path along with file, used in above example:

php basename function example

PHP basename() Function Syntax

The syntax of basename() function in PHP, is:

basename(path, suffix)

The second or the suffix parameter is optional. This parameter is used, when we need to remove the extension of file, so that to get only the file name. For example:

<?php
   $path = "C:\Users\DEV\codescracker.com\myfile.txt";
   echo basename($path, ".txt");
?>

The output of above PHP example, is:

basename function php

PHP Online Test


« Previous Tutorial Next Tutorial »

Like/Share Us on Facebook 😋