Categories
PHP

Capturing the filename from a path in PHP

When you want to isolate the base filename from a path string, this is the way to do it:

[sourcecode language='php']
$path = "/home/project/folder/mypage.php";
$file = basename($path);          // $file is "mypage.php"
$file = basename($path, ".php");      // $file is "mypage"
[/sourcecode]

See dirname() and pathinfo() for related information.

Leave a Reply