Path and Directory Function in PHP

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()

This site runs on the Thesis WordPress Theme

Thesis Theme thumbnail

If you're someone who doesn't understand a lot of PHP, HTML, or CSS, Thesis will give you a ton of functionality without having to alter any code. For the advanced, Thesis has incredible customization possibilities via extensive hooks and filters. And with so many design options, you can use the template over and over and never have it look like the same site.

If you're more familiar with how websites work, you can use the fantastic Thesis User's Guide and world-class support forums to make more professional customizations than you ever thought possible. The theme is not only highly customizable, but it allows me to build sites with a much more targeted focus on monetization than ever before. You can find out more about Thesis below:

{ 3 comments }

1 rajesh October 27, 2009 at 10:56 am

This is fantastic information till now i got through any search engine

2 Chris Brocklesby November 22, 2010 at 8:01 am

Great I have been looking for a solution like this for a while, many Thanks for Sharing… ;)

3 deepak February 16, 2012 at 3:22 am

asdfdvsrtdf

Comments on this entry are closed.

Previous post:

Next post: