17 C
Jaipur
Saturday, March 6, 2021

Use of basename() in PHP

Must read

The basename() function is a built-in function of PHP that retrieves the filename from a given path. It can be used to print only the name of the file from a filename or file path. This function can also be used to print the existing script name. The main purpose of this function is to find out the filename or current script name for any programming purposes. How the basename() function can be used in PHP is shown in this tutorial.

Syntax:
string basename (string $path [, string $suffix ])

This function can take two arguments. The first argument is mandatory and will take filename or filename with the path as a string value. The second argument is optional and is used to get only the filename without extension.

Example1: Read filename from the existing and non-existing filename

The following example shows the use of the basename() function without the optional argument.

Create a PHP file with the following script. Here, the basename() function is used for the existing and non-existing files. Check() function is defined to check if the particular file exists or not. Both hello.txt and world.txt files are used in the basename() function to find out the filename with the extension.

<?php

function Check($file)
{
    if(file_exists($file))
        echo "$file exists.<br />";
    else
        echo "$file does not exist.<br />";
}

//Set the filename that exists
$basepath1 = "hello.txt";

check($basepath1);

//Use of basename() function without optional parameter
echo "<p>The filename with extension is <b>".basename($basepath1) ."</b></p>";

//Set the filename that does not exist
$basepath2 = "world.txt";

check($basepath2);

//Use of basename() function without optional parameter
echo "<p>The filename with extension is <b> ".basename($basepath2) ."</b></p>";

//Use of basename() function with optional parameter
echo "<p><p>The filename without extension is <b>".basename($basepath1,".txt") ."</b></p>";

?>

Output:
The following output will appear after running the above script from the server. The output shows that the hello.txt file exists in the current location, and the basename() function returns the filename. The world.txt file does not exist in the current location, but the basename() function still returns the filename for this file. Thus, the basename() function returns the filename from a file path whether the file exists or not.

Example2: Read filename from the file path

In the previous example, only the filename is passed in the first argument of the basename() function. This example shows the use of the basename() function to find out the filename with an extension and without an extension from the file path. “.php” is used as the optional argument value of the basename() function. If the PHP file exists in the file path, then the basename() function will return the filename without an extension from the path.

<?php

//Set the filepath
$filepath = "var/www/html/php/book.php";

//Retrieve the filename with extension
echo "The name of the file with extension is ";
echo basename($filepath)."<br />";

//Retrieve the filename without extension
echo "The name of the file without extension is ";
echo basename($filepath,".php")."<br />";

?>

Output:
The following output will appear after running the above script from the server. The path that is used in the script, ‘/var/www/html/php/book.php‘, contains a PHP file, and the basename() function returns book.php when used without an optional argument and returns book when it is used with an optional argument.

Example3: Read filename from URL address with query

The following example shows how the basename() function can be used to retrieve the filename from a URL address that contains query variables.

Create a PHP file with the following script. The explode() function is used here to separate the URL and the query string. This function returns an array. The first element of the array contains the URL, and the second element of the array contains the query string value. The basename() function is used to find out the filename from the first element of the array.

<?php

//Set the URL address with query parameter
$url = "http://localhost/php/customer.php?id=108967";

//Retrieve the filepath from the URL
$filepath=explode("?",$url);

//Retrieve the filename with extension
echo "The name of the file with extension is ";
echo basename($filepath[0])."<br />";

?>

Output:
The following output will appear after running the above script from the server. Here, the filename is customer.php.

Example4: Read the directory and the directory after omitting the last directory from the path

The basename() function can also be used to find out the directory name from a path. It is used in the following example to find out the current directory name and the directory name before the current directory from the path.

Create a PHP file with the following script. The $_SERVER[‘PHP_SELF’] is used in the dirname() function to read the full path of the current script, and the basename() function is used to read the directory name that contains this script. When a particular path is defined in the dirname() function, and ‘/’ is used in the second argument of this function, then the path will read the directory path by omitting the last directory name. In this case, the basename() function will return the directory name after omitting the last directory from the path.

<?php

//Read the current directory
$current_dir = basename(dirname($_SERVER[‘PHP_SELF’]),"/");  

//Print the current directory  
echo "The current working directory is : <b> ".$current_dir."</b><br />";

//Read the parent directory of the path
$dir = basename(dirname(‘/var/www/html/php’),"/");

//Print the parent directory name of the path
echo "The previous directory of the given path is : <b> ".$dir."</b>";
?>

Output:
The following output will appear after running the above script from the server.

Example5: Read the current script name

The basename() function can also be used to read the current script name. When __FILE__ is used in the first argument of the basename() function, it will return the script filename as output.

<?php

//Read the current script name
echo "The name of the current script is : <b>".basename(__FILE__)."</b>";

?>

Output:
The following output will appear after running the above script from the server. The output shows the executing script file name.

Conclusion

The basename() function is a useful function of PHP when the coder works with a file or directory for various purposes. Different uses of the basename() function are explained in this tutorial using simple examples to help the readers understand its proper use and apply it in their PHP script.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest article