phpday 2027 - Call for Papers

basename

(PHP 4, PHP 5, PHP 7, PHP 8)

basename — Restituisce il componente finale del nome di un percorso

Descrizione

function basename(string $path, string $suffix = ""): string

Data una stringa contenente il percorso di un file o di una directory, questa funzione restituisce il componente finale del nome.

Nota:

basename() opera in modo ingenuo sulla stringa in ingresso, e non tiene conto del filesystem effettivo, né di componenti del percorso come "..".

Attenzione

basename() tiene conto del locale, quindi affinché restituisca il nome di base corretto con percorsi contenenti caratteri multibyte, occorre impostare il locale corrispondente usando la funzione setlocale(). Se path contiene caratteri non validi per il locale corrente, il comportamento di basename() non è definito.

Elenco dei parametri

path

Un percorso.

Su Windows, sia lo slash (/) che il backslash (\) vengono usati come carattere separatore delle directory. In altri ambienti, si usa lo slash (/).

suffix

Se il componente del nome termina con suffix, anche questo verrà rimosso.

Valori restituiti

Restituisce il nome di base del path indicato.

Esempi

Example #1 Esempio di basename()

<?php
echo "1) ".basename("/etc/sudoers.d", ".d").PHP_EOL;
echo "2) ".basename("/etc/sudoers.d").PHP_EOL;
echo "3) ".basename("/etc/passwd").PHP_EOL;
echo "4) ".basename("/etc/").PHP_EOL;
echo "5) ".basename(".").PHP_EOL;
echo "6) ".basename("/");
?>

Il precedente esempio visualizzerà:

1) sudoers
2) sudoers.d
3) passwd
4) etc
5) .
6)

Vedere anche:

  • dirname() - Restituisce il percorso di una directory padre
  • pathinfo() - Restituisce informazioni su un percorso di file

User Contributed Notes 5 notes

up
56
Anonymous
9 years ago
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 '';
}
up
2
bobray at softville dot com
1 year ago
You might expect that echo basename('directory_name/') would return an empty string. Instead, it returns 'directory_name', without the slash.
up
19
(remove) dot nasretdinov at (remove) dot gmail dot com
18 years ago
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
up
10
swedish boy
17 years ago
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'

?>
up
-2
KOmaSHOOTER at gmx dot de
21 years ago
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    
?>
Adding new notes is disabled. Please see the links in the Found A Problem? section.
To Top