See Also

User Contributed Notes 29 notes

Support of the $suffix parameter has changed between PHP4 and PHP5:in PHP4, $suffix is removed first, and then the core basename is applied.conversely, in PHP5, $suffix is removed AFTER applying core basename.

There is a problem reading non-Latin characters in the file name if the locale is not configured correctly.For example: instead of the name «ФЫВА-1234.doc», is displayed «-1234.doc».Solution: rawurldecode(basename($full_name)).

There is a real problem when using this function on *nix servers, since it does not handle Windows paths (using the \ as a separator). Why would this be an issue on *nix servers? What if you need to handle file uploads from MS IE? In fact, the manual section "Handling file uploads" uses basename() in an example, but this will NOT extract the file name from a Windows path such as C:\My Documents\My Name\filename.ext. After much frustrated coding, here is how I handled it (might not be the best, but it works):

As already pointed out above, if a query string contains a '/' character, basename will not handle it well. But it can come really handy when you want to pass a url with query string to a funtion that copies/downloads the file using basename as local filename, by attaching an extra query to the end of the url:

icewinds exmaple wouldn't work, the query part would contain the second char of the filename, not the query part of the url.<?$file = "path/file.php?var=foo";$file = explode("?", basename($file));$query = $file[1];$file = $file[0];?>

if you want the name of the parent directory<?php$_parenDir_path = join(array_slice(split( "/" ,dirname($_SERVER['PHP_SELF'])),0,-1),"/").'/'; // returns the full path to the parent dir$_parenDir = basename ($_parenDir_path,"/"); // returns only the name of the parent dir// or $_parenDir2 = array_pop(array_slice(split( "/" ,dirname($_SERVER['PHP_SELF'])),0,-1)); // returns also only the name of the parent direcho('$_parenDir_path = '.$_parenDir_path.'<br>');echo('$_parenDir = '.$_parenDir.'<br>');echo('$_parenDir2 = '.$_parenDir2.'<br>');?>