★ wanayoo — archive 1999 http://uk.php.net/manual/cs/function.pathinfo.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<parse_ini_filepclose>
view the version of this page
Last updated: Wed, 20 Aug 2003

pathinfo

(PHP 4 >= 4.0.3)

pathinfo -- Returns information about a file path

Description

array pathinfo ( string path)

pathinfo() returns an associative array containing information about path. The following array elements are returned: dirname, basename and extension.

Příklad 1. pathinfo() Example

<?php

$path_parts = pathinfo("/www/htdocs/index.html");

echo $path_parts["dirname"] . "\n";
echo $path_parts["basename"] . "\n";
echo $path_parts["extension"] . "\n";

?>

Would produce:

/www/htdocs
index.html
html

Poznámka: For information on retrieving the current path info, read the section on predefined reserved variables.

See also dirname(), basename(), parse_url() and realpath().



add a note add a note User Contributed Notes
pathinfo
sergey_at_work at hotmail dot com
09-Jan-2003 08:02
Note this:
if you use $path_parts = ("/cd/"); and error_reporting(E_ALL)
you will get "Notice: Undefined index: extension in d:\www\test.php on line 7"

Use isset() function to correct this problem
albertof at deltasoft dot com dot ar
29-May-2002 06: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];

          }
    }
phpnet at cuntbubble dot com
21-Jan-2002 02:29
this function isn't very well tested

here's my working version (well on FreeBSD with 4.1.1 in mod_php4 on Apache anyway)
====================
function crackpath($path) {
 $sep = strpos($path, "/") > -1 ? "/" : "\\";
 $parts = explode($sep, $path);
 $file = $parts[sizeof($parts)-1];
 $pi = pathinfo($file);
 $dir = implode($sep, array_slice($parts, 0, -1));
 (!$dir && $file) && $dir = ".";
 $lenex = strlen($pi["extension"]);
 if($lenex)
  $pi["basename"] = substr($file, 0, - ($lenex+1));
 else
  $file && $pi["basename"] = $file;

 $pi["basename"];
 $dir && $pi["dirname"] = $dir ? $dir : ".";

 return $pi;
}
========================
print_r(pathinfo("/foo/bar.bar/baz"));
/* wrong answer
Array
(
    [dirname] => /foo/bar.bar
    [basename] => baz
    [extension] => bar/baz
)
*/

print_r(crackpath("/foo/bar.bar/baz"));
/* right answer
Array
(
    [dirname] => /foo/bar.bar
    [basename] => baz
)
*/

print_r(pathinfo("/foo/bar.bar/baz/"));
/* wrong again
Array
(
    [dirname] => /foo/bar.bar
    [basename] => baz
    [extension] => bar/baz/
)
*/

print_r(crackpath("/foo/bar.bar/baz/"));
/* right
Array
(
    [dirname] => /foo/bar.bar/baz
    [basename] =>
)
*/

print_r(crackpath("\\foo\\bar.bar\\baz"));
/* ooh i do windows too
Array
(
    [dirname] => \foo\bar.bar
    [basename] => baz
)
*/

print_r(pathinfo(""));
/* bah don't use array_key_exists to check for filename !
Array
(
    [basename] =>
)
*/

print_r(crackpath(""));
/* better copy this "broken" functionality
Array
(
    [basename] =>
)
*/

print_r(pathinfo("/foo/bar."));
/* interesting
Array
(
    [basename] => bar.
    [dirname] => foo
)
*/

print_r(crackpath(""));
/* better copy this too
Array
(
    [basename] => bar.
    [dirname] => foo
)
*/
m-symons at home dot com
25-Aug-2001 12: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
24-Aug-2001 11: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 03: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.

<parse_ini_filepclose>
 Last updated: Wed, 20 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: Kewlio.net Limited
Last updated: Mon Nov 17 01:11:52 2003 GMT