|
|
 |
basename (PHP 3, PHP 4, PHP 5) basename -- Returns filename component of path Descriptionstring basename ( string path [, string suffix] )
Given a string containing a path to a file, this function will
return the base name of the file.
If the filename ends in suffix this will
also be cut off.
On Windows, both slash (/) and backslash
(\) are used as directory separator character. In
other environments, it is the forward slash
(/).
Example 1. basename() example |
<?php
$path = "/home/httpd/html/index.php";
$file = basename($path); $file = basename($path, ".php"); ?>
|
|
Note:
The suffix parameter was added in PHP 4.1.0.
See also dirname().
User Contributed Notes
basename
KOmaSHOOTER at gmx dot de
30-Jan-2005 08:18
if you want the name of the parent directory
<?php
$_parenDir_path = join(array_slice(split( "/" ,dirname($_SERVER['PHP_SELF'])),0,-1),"/").'/'; $_parenDir = basename ($_parenDir_path,"/"); $_parenDir2 = array_pop(array_slice(split( "/" ,dirname($_SERVER['PHP_SELF'])),0,-1)); echo('$_parenDir_path = '.$_parenDir_path.'<br>');
echo('$_parenDir = '.$_parenDir.'<br>');
echo('$_parenDir2 = '.$_parenDir2.'<br>');
?>
KOmaSHOOTER at gmx dot de
29-Jan-2005 06:24
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']),"/"));
?>
Example:
www dir: domain.com/temp/2005/january/t1.php
<?php
echo('dirname <br>'.dirname($_SERVER['PHP_SELF']).'<br><br>');
?>
<?php
echo('file = '.basename ($PHP_SELF,".php"));
?>
if you combine these two you get this
<?php
echo('dir = '.basename (dirname($_SERVER['PHP_SELF']),"/"));
?>
And for the full path use this
<?php
echo(' PHP_SELF <br>'.$_SERVER['PHP_SELF'].'<br><br>');
?>
antrik at users dot sf dot net
15-Nov-2004 12:40
When using basename() on a path to a directory ('/bar/foo/'), the last path component ('foo') is returned, instead of the empty string one would expect. (Both PHP 4.1.2 and 4.3.8 on GNU/Linux.) No idea whether this is considered a bug or a feature -- I found it extremely annoying. Had to work around using: <?php $file=substr($path, -1)=='/'?'':basename($path) ?> Watch out!
osanim at cidlisuis dot org
17-Apr-2004 01:12
If you want know the real directory of the include file, you have to writte:
<?php
dirname(__FILE__)
?>
KOmaSHOOTER at gmx dot de
28-Nov-2003 04:33
Exmaple for exploding ;) the filename to an array
<?php
echo(basename ($PHP_SELF)."<br>"); $file = basename ($PHP_SELF);
$file = explode(".",$file);
print_r($file); echo("<br>");
$filename = basename(strval($file[0]),$file[1]);
echo($filename."<br>"); echo(basename ($PHP_SELF,".php")."<br>"); echo("<br>");
echo("<br>");
show_source($file[0].".".$file[1])
?>
giovanni at giacobbi dot net
08-Nov-2003 09:52
No comments here seems to take care about UNIX system files, which typically start with a dot, but they are not "extensions-only".
The following function should work with every file path. If not, please let me know at my email address.
<?php
function remove_ext($str) {
$noext = preg_replace('/(.+)\..*$/', '$1', $str);
print "input: $str\n";
print "output: $noext\n\n";
}
remove_ext("/home/joh.nny/test.php");
remove_ext("home/johnny/test.php");
remove_ext("weirdfile.");
remove_ext(".hiddenfile");
remove_ext("../johnny.conf");
daijoubu_NOSP at M_videotron dot ca
16-Oct-2003 12:22
An faster alternative to:
<?php
array_pop(explode('.', $fpath));
?>
would be:
<?php
substr($fpath, strrpos($fpath, '.')); ?>
If you don't want the dot, simply adds 1 to the position
<?php
substr($fpath, strrpos($fpath, '.') + 1); ?>
travis dot kroh at ndsu dot nodak dot edu
19-May-2003 04:12
<?php
$foo="/path/to/file.ext";
echo $foo."<br />";
$bar=$foo;
echo basename($bar,".ext")."<br />";
echo $foo."<br />";
echo $bar."<br />";
?>
Will display:
/path/to/file.ext
file
/path/to/file
/path/to/file
Which is probably not expected behavior, as
it returns one thing, changes the variable
to produce something else, and also modifies
BOTH variables in memory.
To avoid this, use:
basename(strval($bar),".ext")
Richard at Lyders dot Net
01-Apr-2003 03:53
you can also make use of the basename() function's second parameter:
<?PHP
$fpath = "/blah/file.name.has.lots.of.dots.ext";
$fext = array_pop(explode('.', $fpath));
$fname = basename($fpath, '.'.$fext);
print "fpath: $fpath\n<br>";
print "fext: $fext\n<br>";
print "fname: $fname\n<br>";
?>
| |