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

Information and Links

Join the fray by commenting, tracking what others have to say, or linking to it from your blog.


Similar Entries
MySQL Queries Made Easy With PHP Functions Library
Every web developer has a library of code that they reference frequently, if not constantly. I'm sharing with you one of my most useful snippets my MySQL helper class that helps me organize my queries and easily reference commonly used functions.
Multiple Select Drop Down Menu in PHP
It's very easy to take what we did back in January for reusable PHP drop down menu code and make it work for multi value select elements.
.htaccess Redirect a Directory to a Subdomain and Force WWW
It's really easy to force the www for your website URL and to redirect a directory properly to a new subdomain address. Just give it a shot, organize your directories!
PHP State Drop Down Menu - Reusable Code!
Drop down or select menu's are a common form item that is used frequently in the CMS', registration forms and lead forms I build. In this entry I've explored the way I find to most effectively display frequently used drop downs: US states, months and days of the week.
Swapping Two Variables in One Line With PHP
Swap two variables in one line without using a temporary variable. The all-time popular interview or "I'm more programmer than you" question: swapping two variables without using a temporary variables.
Next Post
Event Observe and Event Listeners with Prototype
Previous Post
Repair and Upgrade my Inspiron 600M

Write a Comment

Take a moment to comment and tell us what you think. Some basic HTML is allowed for formatting.

Reader Comments

Be the first to leave a comment!