basename
(PHP 4, PHP 5)
basename — Returns trailing name component of path
Description
$path
[, string $suffix
] )Given a string containing the path to a file or directory, this function will return the trailing name component.
Parameters
-
path
-
A path.
On Windows, both slash (/) and backslash (\) are used as directory separator character. In other environments, it is the forward slash (/).
-
suffix
-
If the name component ends in
suffix
this will also be cut off.
Return Values
Returns the base name of the given path
.
Changelog
Version | Description |
---|---|
4.1.0 |
The suffix parameter was added
|
Examples
Example #1 basename() example
<?php
echo "1) ".basename("/etc/sudoers.d", ".d").PHP_EOL;
echo "2) ".basename("/etc/passwd").PHP_EOL;
echo "3) ".basename("/etc/").PHP_EOL;
echo "4) ".basename(".").PHP_EOL;
echo "5) ".basename("/");
?>
The above example will output:
1) sudoers 2) passwd 3) etc 4) . 5)
Notes
Note:
basename() operates naively on the input string, and is not aware of the actual filesystem, or path components such as "..".
Note:
basename() is locale aware, so for it to see the correct basename with multibyte character paths, the matching locale must be set using the setlocale() function.
See Also
- dirname() - Returns parent directory's path
- pathinfo() - Returns information about a file path
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения для работы с файловой системой
- Функции для работы с файловой системой
- basename
- chgrp
- chmod
- chown
- clearstatcache
- copy
- delete
- dirname
- disk_free_space
- disk_total_space
- diskfreespace
- fclose
- feof
- fflush
- fgetc
- fgetcsv
- fgets
- fgetss
- file_exists
- file_get_contents
- file_put_contents
- file
- fileatime
- filectime
- filegroup
- fileinode
- filemtime
- fileowner
- fileperms
- filesize
- filetype
- flock
- fnmatch
- fopen
- fpassthru
- fputcsv
- fputs
- fread
- fscanf
- fseek
- fstat
- ftell
- ftruncate
- fwrite
- glob
- is_dir
- is_executable
- is_file
- is_link
- is_readable
- is_uploaded_file
- is_writable
- is_writeable
- lchgrp
- lchown
- link
- linkinfo
- lstat
- mkdir
- move_uploaded_file
- parse_ini_file
- parse_ini_string
- pathinfo
- pclose
- popen
- readfile
- readlink
- realpath_cache_get
- realpath_cache_size
- realpath
- rename
- rewind
- rmdir
- set_file_buffer
- stat
- symlink
- tempnam
- tmpfile
- touch
- umask
- unlink
Коментарии
If you want the current path where youre file is and not the full path then use this :)
<?php
echo('dir = '.basename (dirname($_SERVER['PHP_SELF']),"/"));
// retuns the name of current used directory
?>
Example:
www dir: domain.com/temp/2005/january/t1.php
<?php
echo('dirname <br>'.dirname($_SERVER['PHP_SELF']).'<br><br>');
// returns: /temp/2005/january
?>
<?php
echo('file = '.basename ($PHP_SELF,".php"));
// returns: t1
?>
if you combine these two you get this
<?php
echo('dir = '.basename (dirname($_SERVER['PHP_SELF']),"/"));
// returns: january
?>
And for the full path use this
<?php
echo(' PHP_SELF <br>'.$_SERVER['PHP_SELF'].'<br><br>');
// returns: /temp/2005/january/t1.php
?>
There is only one variant that works in my case for my Russian UTF-8 letters:
<?php
function mb_basename($file)
{
return end(explode('/',$file));
}
><
It is intented for UNIX servers
Here is a quick way of fetching only the filename (without extension) regardless of what suffix the file has.
<?php
// your file
$file = 'image.jpg';
$info = pathinfo($file);
$file_name = basename($file,'.'.$info['extension']);
echo $file_name; // outputs 'image'
?>
It's a shame, that for a 20 years of development we don't have mb_basename() yet!
// works both in windows and unix
function mb_basename($path) {
if (preg_match('@^.*[\\\\/]([^\\\\/]+)$@s', $path, $matches)) {
return $matches[1];
} else if (preg_match('@^([^\\\\/]+)$@s', $path, $matches)) {
return $matches[1];
}
return '';
}