Basename in PHP | PHP basename() Function

Introduction

PHP has a function called basename that helps to fetch the filename present at the specified path. The function returns and prints the filename or the directory path passed in arguments. The procedure also displays the script name if the basename used is $0. Programmers use this function to check the files present at the specified location or use the program flow filenames.

Hence, the programmers use the basename function in PHP to find the files present at any location and get them printed on the console. The basename function is like any other built-in function or user-defined function in PHP, such as string functions, math functions, numeric functions, date functions, and many more.

Basename Function in PHP

The basename is an in-built function in PHP, which returns the file name present at the path provided as an argument.

Syntax:

Below is the syntax to use the Basename function in PHP.

String basename($path, $suffix)

The function has two parameters, i.e., path and suffix.

  • $path: Path is a mandatory parameter and s of String type. This parameter specifies the path at which the file is to locate.
  • $suffix: Suffix is an optional parameter that hides the file extension if the file has a suffix.

The function returns the file basename, which is present at the path passed as $path in the basename parameter.

Examples of Basename in PHP

Example1: The code prints the name of the file present at a specified location without using the optional parameter $suffix.

<?php

// Save the path in the $path variable

$path = “/ExampleProject1/Example1.php”;

// basename function finds the name of the file present at $path and saves it in //$fileName variable

$fileName = basename($path);

// Prints the filename

Echo $fileName;

Echo “\n”

?>

Output:

Example1.php

Here, only the $path parameter is passed with the basename function call, so the complete file name gets printed.

Example2: The code prints the file’s name at the specified location when the optional parameter $suffix is passed.

<?php

// Save the path in the $path variable

$path = “/ExampleProject1/Example1.php”;

// Using the $suffix parameter so that the output contains only the filename, without //extension

$fileName = basename($path, “.php”);

Echo $fileName;

?>

Output:

Example1

Here, $suffix is passed with a function call, so only the file name gets printed without extension.

Example3: The code prints the name of the file present at the specified location with both versions of the basename function.

<?php

// Save the path in the $path variable

$path = “/Project1/team1/FirstProgram.php”;

// Using the $path parameter with basename function

$fileName = basename($path);

Echo $fileName;

Echo “\n” // It takes the cursor to next line

// Using the $path parameter with basename function

$fileName = basename($path, “.php”);

Echo $fileName;

?>

Output:

FirstProgram.php

FirstProgram

When only the $path is passed as a parameter, the complete filename gets printed. When the $suffix .php is passed with a function call, the .php gets hidden and only filename gets printed.

Errors and Exception in Basename in PHP

It is indeed necessary to understand the functioning and usage of basename function in PHP, but it is also crucial to understand the exceptions to use the process accurately:

Basename function in PHP

Cannot identify the $path value given as ‘..’; however, it can recognise the single dot ‘.’. Double beads ‘..’ are only used in Linux that move the location from the current directory to the previous directory. As double dots ‘..’ is used in Linux only, so basename in PHP only recognises the single drop ‘.’.

Basename function in PHP

Uses the $path passed as string and is not aware of the file system. It implies that the process only works on the way passed as a parameter irrespective of the filesystem type. The file system can be Mac, Linux, and Windows. As each of these file systems has a different format, and basename function cannot recognise the type of filesystem used. Therefore, the output can get wrong. Hence, it is recommended to pass parameters in the basename function.

Slashes are used as the separator in the directory path or to separate the folders. Windows platform can recognise both backslash (\) and forward-slash (/) as a separator in the directory path, while in other environments, only forward-slash (/) gets used. So, we should use slash cautiously while using the basename function in PHP.

Conclusion

This was all about the basename function in PHP. We hope you have a deep understanding of PHP’s basename, usage, syntax, and different exceptions. The exceptions should be considered before using the basename function so that the correct output can be expected from it.

If you’re interested to learn more about full-stack software development, check out upGrad & IIIT-B’s PG Diploma in Full-stack Software Development, which is designed for working professionals and offers 500+ hours of rigorous training, 9+ projects, and assignments, IIIT-B Alumni status, practical hands-on capstone projects & job assistance with top firms.

Become a Full Stack Developer

UPGRAD AND IIIT-BANGALORE'S PG DIPLOMA IN FULL STACK SOFTWARE DEVELOPMENT
LEARN MORE@UPGRAD

Leave a comment

Your email address will not be published.

×