One thing I find myself doing constantly in PHP is exploding strings on ‘/’ and then picking out pieces. Then one day while perusing some code in a WordPress Plugin someone had built I realized that I was making things entirely too hard for myself.
Directory Name – dirname()
This is an easy one, it returns the name of the directory the specified file is in.
Here’s two examples for you
<?php
$path = "/html/index";
$dir = dirname($path); //$dir is set to "/html"
$path = "/home/httpd/domains/example.org/html/index.php";
$dir = dirname($path); //$dir is set to "/home/httpd/domains/example.org/html"
?>
Though be warned if there is not file then the last directory is not listed and is instead recognized as the file
<?php
$path = "/html/";
$dir = dirname($path); //$dir is set to "/"
$path = "/home/httpd/domains/";
$dir = dirname($path); //$dir is set to "/home/httpd"
?>
Read more in the PHP.net Manual – dirname()
File Name – basename()
Basename pulls out what dirname misses, the actual file being called
<?php
$path = "/home/httpd/domains/example.org/html/index.php";
$file = basename($path); // $file is set to "index.php"
$path = "/html/index";
$file = basename($path); // $file is set to "index"
?>
This function also has an optional parameter to remove a specified suffix, like so:
<?php
$path = "/home/httpd/domains/example.org/html/index.php";
$file = basename($path, '.php'); // $file is set to "index"
?>
Read more in the PHP.net Manual – basename()
Path Information – pathinfo()
Path Info will give you any information you need about a specified path. It returns an array of information or if you pass it the option of what specific information you want in the second (optional) parameter it returns a string.
<?php
$path_parts = pathinfo('/home/httpd/domains/example.org/html/index.php');
$dir = $path_parts['dirname']; //$dir is set to "/home/httpd/domains/example.org/html/" -- same as dirname($path_parts)
$basename = $path_parts['basename']; //$dir is set to "index.php" -- same as basename($path_parts)
$ext = $path_parts['extension']; //$ext is set to "php"
$file = $path_parts['filename']; //$file is set to "index"
?>
My favorite thing about pathinfo() is the ‘filename’ key of the array. If I pass it a path that has a period in the filename it detects it correctly.
<?php
$path_parts = pathinfo('/home/httpd/domains/example.org/html/index.344.php');
$basename = $path_parts['basename']; //$dir is set to "index.344.php" -- same as basename($path_parts)
$ext = $path_parts['extension']; //$ext is set to "php"
$file = $path_parts['filename']; //$file is set to "index.344"
?>
Read more in the PHP.net Manual – pathinfo()
Real Path – realpath()
Realpath is a great function that gives you the full absolute path to the directory of your choice in relation to the directory the file is in.
If my file is located in the absolute path of ‘/home/httpd/domains/example.org/html/‘ then the following would happen usign the realpath function.
<?php
$new_path = realpath('./'); //$new_path is set to "/home/httpd/domains/example.org/html/"
$new_path = realpath('./../../subdomain.example.net/html/includes'); //$new_path is set to "/home/httpd/domains/subdomain.example.net/html/includes"
?>
I really like that one. I’ll have to do some more testing to see if it gets messy when files are being included or when using Apache’s mod rewrite like some $_SERVER variables do.
Read more in the PHP.net Manual – realpath()
{ 1 comment… read it below or add one }
This is fantastic information till now i got through any search engine