Search code examples
phpsubstringfilenamesfilepathtext-extraction

Extract filename with extension from filepath string


I am looking to get the filename from the end of a filepath string, say

$text = "bob/hello/myfile.zip";

I want to be able to obtain the file name, which I guess would involve getting everything after the last slash as a substring. Can anyone help me with how to do this is PHP? A simple function like:

$fileName = getFileName($text);

Solution

  • Check out basename().