suche nach in der

pclose> <parse_ini_string
Last updated: Fri, 18 May 2012

view this page in

pathinfo

(PHP 4 >= 4.0.3, PHP 5)

pathinfoLiefert Informationen über einen Dateipfad

Beschreibung

mixed pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )

pathinfo() gibt Informationen über einen Dateipfad (path) zurück: entweder als assoziatives Array oder als String, abhängig vom options-Parameter.

Parameter-Liste

path

Der zu analysierende Dateipfad.

options

Falls angegeben, wird nur dieses eine Element zurückgegeben. Mögliche Werte: PATHINFO_DIRNAME, PATHINFO_BASENAME, PATHINFO_EXTENSION oder PATHINFO_FILENAME.

Falls options nicht angegeben wird, werden alle verfügbaren Elemente zurückgegeben.

Rückgabewerte

Falls der options-Parameter nicht angegeben wird, wird ein assoziatives Array mit den folgenden Elementen zurückgegeben: dirname, basename, extension (falls vorhanden) und filename.

Hinweis:

Falls path keine Erweiterung hat, wird das extension-Element nicht zurückgegeben (siehe das zweite Beispiel unten).

Falls options genutzt wird, gibt diese Funktion einen String mit dem gewünschten Element zurück.

Changelog

Version Beschreibung
5.2.0 Die PATHINFO_FILENAME-Konstante wurde hinzugefügt.

Beispiele

Beispiel #1 pathinfo()-Beispiel

<?php
$path_parts 
pathinfo('/www/htdocs/inc/lib.inc.php');

echo 
$path_parts['dirname'], "\n";
echo 
$path_parts['basename'], "\n";
echo 
$path_parts['extension'], "\n";
echo 
$path_parts['filename'], "\n"// seit PHP 5.2.0
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

/www/htdocs/inc
lib.inc.php
php
lib.inc

Beispiel #2 pathinfo()-Beispiel für den Unterschied zwischen einer leeren und keiner Erweiterung

<?php
$path_parts 
pathinfo('/path/emptyextension.');
var_dump($path_parts['extension']);

$path_parts pathinfo('/path/noextension');
var_dump($path_parts['extension']);
?>

Das oben gezeigte Beispiel erzeugt eine ähnliche Ausgabe wie:

string(0) ""

Notice: Undefined index: extension in /test.php on line 6
NULL

Anmerkungen

Hinweis:

Informationen über das Wiederauffinden der aktuellen Pfadinformation finden Sie unter Vordefinierte Variablen.

Hinweis:

pathinfo() berücksichtigt die locale-Einstellung. Um einen Pfad, der Multibyte-Zeichen enthält, korrekt parsen zu können, muss die entsprechende locale mit der setlocale()-Funktion gesetzt werden.

Siehe auch

  • dirname() - Beschreibung
  • basename() - Gibt letzten Namensteil einer Pfadangabe zurück
  • parse_url() - Analysiert einen URL und gibt seine Bestandteile zurück
  • realpath() - Löst einen Pfad in einen absoluten und eindeutigen auf



add a note add a note User Contributed Notes
pathinfo
avi-team at inbox dot lv
15-Jul-2007 05:14
You shouldn't assign values as it is described in previous post
// wrong:
list( $dirname, $basename, $extension, $filename ) = array_values( pathinfo($file) );

if $file has no extension, you get wrong variable values: $extension would be assigned with 'filename' array element of pathinfo() result, but $filename - would be empty.
phpnet at whoisgregg dot com
30-May-2007 08:01
If you want to easily assign the values returned by pathinfo to separate variable names, list isn't enough. You can use array_values() first to convert the associative array into the indexed array that list() expects:

// throws notices, variables aren't set
list( $dirname, $basename, $extension, $filename ) = pathinfo($file);

// works
list( $dirname, $basename, $extension, $filename ) = array_values( pathinfo($file) );
cochise_chiracahua at hotmail.com
25-Nov-2005 08:55
Sometimes, it's interessant to get the basename without extension.
So, I appended a new entry 'basenameWE' (Basename Without Extension) to the returned array.

<?php

// pathinfo improved
function pathinfo_im($path) {
   
   
$tab = pathinfo($path);
   
   
$tab["basenameWE"] = substr($tab["basename"],0
   
,strlen($tab["basename"]) - (strlen($tab["extension"]) + 1) );
   
    return
$tab;
}

$my_path = "/var/www/html/example.html";

echo
"<pre>\n";
print_r( pathinfo_im($my_path) );
echo
"</pre>\n";

?>

Out :

Array
(
    [dirname] => /var/www/html
    [basename] => example.html
    [extension] => html
    [basenameWE] => example
)
sgermain at icexnetworks dot com
08-Jul-2005 08:24
It is true that if you put a directory into pathinfo, usually the extension is empty. But, if the directory name is /www/example.com/ for example, you will have the following output:

Array
(
    [dirname] => /www
    [basename] => example.com
    [extension] => com
)

So, it is the same as a file.
n0dalus
08-Feb-2005 10:47
If a file has more than one 'file extension' (seperated by periods), the last one will be returned.
For example:
<?php
$pathinfo
= pathinfo('/dir/test.tar.gz');
echo
'Extension: '.$pathinfo['extension'];
?>
will produce:
Extension: gz

and not tar.gz
03-Dec-2004 01:39
If you want only the file extension, use this:
<?php
$extension
= substr(strrchr($filename, "."), 1);
?>
This is many times faster than using pathinfo() and getting the value from array.
rob at webdimension dot co dot uk
04-Oct-2004 03:48
Further to my previous post.

This affects servers that run PHP as a cgi module

If you have your own server:
You can use the AcceptPathInfo directive to force the core handler to accept requests with PATH_INFO and thereby restore the ability to use PATH_INFO in server-side includes.

Further information:
http://httpd.apache.org/docs-2.0/mod/core.html#acceptpathinfo
junk at plaino dot com
19-Aug-2004 03:41
Convert a URL to the local file path and vice versa, convert a local file path to a URL.

// this sets the sytem / or \ :
strstr( PHP_OS, "WIN") ? $slash = "\\" : $slash = "/";

// This is the location of the php file that contains this
// function. Usually this request is made to files/folders
// down the directory structure, so the php file that
// contains these functions is a good "where am i"
// reference point:
$WIMPY_BASE['path']['physical'] = getcwd();
$WIMPY_BASE['path']['www'] = "http://".$_SERVER['HTTP_HOST'];

function url2filepath($theURL){
    global $WIMPY_BASE, $slash;
    $AtheFile = explode ("/", $theURL);
    $theFileName = array_pop($AtheFile);
    $AwimpyPathWWW = explode ("/", $WIMPY_BASE['path']['www']);
    $AtheFilePath = array_values (array_diff ($AtheFile, $AwimpyPathWWW));
    if($AtheFilePath){
        $theFilePath = $slash.implode($slash, $AtheFilePath).$slash.$theFileName;
    } else {
        $theFilePath = implode($slash, $AtheFilePath).$slash.$theFileName;
    }
    return ($WIMPY_BASE['path']['physical'].$theFilePath);
}

function filepath2url ($theFilepath){
    global $WIMPY_BASE, $slash;
    $AtheFile = explode ($slash, $theFilepath);
    $theFileName = array_pop($AtheFile);
    $AwimpyPathFILE = explode ($slash, $WIMPY_BASE['path']['physical']);
    $AtheFilePath = array_values (array_diff ($AtheFile, $AwimpyPathFILE));
    $thFileURL = implode("/", $AtheFilePath)."/".$theFileName;
    return ($WIMPY_BASE['path']['www']."$thFileURL");
}
albertof at deltasoft dot com dot ar
29-May-2002 10:10
This code is to work in index.php/var/var

if(isset($PATH_INFO)) {
      $viewcode = explode('/', $PATH_INFO);
        $num = count($viewcode);
        if($num % 2 == 0) {
            $viewcode[] = '';
            $num++;
        }
        for($i = 1; $i < $num; $i += 2) {

            $$viewcode[$i] = $viewcode[$i+1];

          }
    }
m-symons at home dot com
25-Aug-2001 04:01
And, of course, to account for the problem noted in the first post whereby calling a directory, not a file, messes with the output of pathinfo(), you can include the following test:

if($pathinfo[extension] == "") {

$deep++;

}

Ooops...sorry for missing that.
m-symons at home dot com
25-Aug-2001 03:54
Here's a neat wee function to grab the relative path to root (especially useful if you're using mock-directories to pass variables into scripts with mod_rewrite).  The function simply iterates through every occurence of "/" within the REQUEST_URI environment variable, appending "../" to the output for every instance:

<?php

function path_to_root($path) {

   
$pathinfo = pathinfo($path);
   
   
$deep = substr_count($pathinfo[dirname], "/");
   
   
$path_to_root = "./";
   
    for(
$i = 1; $i <= $deep; $i++) {
   
       
$path_to_root .= "../";
       
    }
   
    return
$path_to_root;
}

path_to_root($REQUEST_URI);

?>
mikep at oeone dot com
22-Aug-2001 07:27
If you run this on a directory, basename is the last directory in the path, dirname is the path before the final directory and extension is empty.

pclose> <parse_ini_string
Last updated: Fri, 18 May 2012