jump to navigation

Parsing Directory Paths and Filenames with PHP November 12, 2011

Posted by Tournas Dimitrios in PHP.

Sometimes , your need to know relative path for coding ‘ include() ‘ or ‘ require() ‘ in PHP but your script resides in different sub directories and different levels and it got messing . How could you detect the Relative Path and Absolute Path on your document root ? And later use single variables or constant and specify relative path of included script relative to document root instead …
It’s often useful to parse directory paths for various parts , such as the tailing extension name , directory component , and base name . Several functions are available for performing such tasks , all of which are introduced in this article .

  • basename() : This  function in PHP will return the base name of the file or path you point it at . You can use this function with one parameter or two . The first parameter is the target file or path , and the second is the suffix . If the base name ends with a suffix you may choose to remove it using the second parameter.
    Here is the Example:
    $file = “phpexamples/filename.txt”;
    // Using one parameter
    $baseName = basename($file);
    echo $baseName ; //returns filename.txt
    // Using two parameters, the second to cut the suffix
    $baseName = basename($file, “.txt”);
    echo $baseName ;// returns filename
  • dirname() :  This function is essentially the counterpart to basename() , providing the directory component of the path :
    $path = “/home/www/phpexamples/file.txt” ;
    $dirname = dirname($path) ;
    // $dirname contains “/home/www/phpexamples”
  • pathinfo() :   This  function is often used by programmers to identify the types of files being specified in URLs . Pathinfo will do simple parsing of path and filenames , then present an array of useful attributes such as the base name of the file specified or the file extension of the file specified . The following example is provided from the PHP.net website :

    $path_parts = pathinfo(‘/www/htdocs/index.html’) ;
    echo $path_parts[‘dirname’], “\n” ; //returns  /www/htdocs
    echo $path_parts[‘basename’], “\n” ; // returns  index.html
    echo $path_parts[‘extension’], “\n” ; // returns html
    echo $path_parts[‘filename’], “\n”; // since PHP 5.2.0 : returns index

  • realpath()  :    This function converts all symbolic links , and relative path references located in path , to their absolute counterparts 
    echo realpath(‘.’);
    //output : c:\wamp\www
    echo realpath(‘../folder1’);
    //output : c:\wamp\folder1
    include realpath(‘../myIncludeFile.php’);
    //include the other directory file
  • getcwd()  : Gets the current working directory
    // current directory
    echo getcwd() . “\n” ; //returns /home/didou
    chdir(‘cvs’) ;
    // current directory
    echo getcwd() . “\n” ; //returns /home/didou/cvs

In PHP there  is a global variable (array)  $_SERVER[‘ ‘] that  contains information  such as headers, paths and current script locations  . Depending on the environment your server is running PHP in , will determine what variables you have access to .  The entries in this array( ca 25 records ) are created by the web server . For example…. $_SERVER[‘DOCUMENT_ROOT’] …. will display the document root of where you are running the code from .  Some records that may be useful for this article could be :

  • $_SERVER[REQUEST_URI] => /test/displaypath.php
  • $_SERVER [SCRIPT_NAME] => /test/displaypath.php
  • $_SERVER[PHP_SELF] => /test/displaypath.php
  • $_SERVER[HTTP_HOST] => yourdomain.com
  • $_SERVER[SERVER_NAME] => yourdomain.com
  • $_SERVER[SCRIPT_FILENAME] => /www/yourdomain.com/test/displaypath.php

If you want to look at all fields in $_SERVER variable , place the following PHP script in the web server and access it using a browser .

<title>$_SERVER variable information
<h1>Dump of $_SERVER variable</h1>
<?php print_r($_SERVER); ?>

And finally ,  there are a couple PHP “magic constants” (or “magical contants”) which can be useful for parsing directory paths or filenames  ( _ _FILE_ _  and _ _DIR_ _ )


No comments yet — be the first.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s