basename() function | Get file & folder name from path in PHP

basename() is an inbuilt function in PHP. It is used to get the name of the file from a path. You can pass the path in its argument as a string and then it will return the name of the file with the extension.

Basically, the basename() function splits path and return last data from it. But it does not create an array.

<?php

echo basename("C:\Users\pc\Desktop\a.png");

?>

Even you can get the last directory name from path with this function

<?php

echo basename("C:\Users\pc\Desktop");

?>

Leave a Comment