Tuesday, January 18, 2011

How to find your absolute path of a file or folder

What is the absolute path?

The absolute path is the directory location on a server's hard drive where Joomla! is located.
Ex:

    $path = getcwd();
    echo "Your Absoluthe Path is: ";
    echo $path;
 ?>
Another Way to find the absolute Path
$dir = dirname(__FILE__);
echo "Full path to this dir: " . $dir . "
";
echo "Full path to a .htpasswd file in this dir: " . $dir . "/.htpasswd" . "
";
?>

Absolute path and Relative path file inclusion in PHP


I think you guyz must be aware of absolute path and relative path in PHP. If you do not know anything about it then let me explain you about absolute path and relative path in the server with the example using include() function of PHP. You can see the example below, I’ve included the same files in PHP but the same file is included in two different manner.
  include("/home/example/public_html/config.php"); //absolute path
  include("config.php"); //relative path
As you can see that in the first line, config.php file is included using absolute path in which is full path of the file in the server. The next line uses relative path file inclusion.
Well I’ve been using the relative path inclusion in the beginning of my career and I’ve faced some serious problem in a project.Since the config.php has be be included inside many files, I’ve give the various paths from the various folders which should be relative to the root folder and which was quite hectic.
So what is the solution. The solution is to use absolute path file inclusion.Absolute path of the file is always same no matter from which folder you include that file in server.
How to get absolute Path in PHP?? The absolute path of your local server and the real server might be different. So to get the absolute path for both version, you can take the help of the magic constant called __FILE__ anddirname() function available in PHP.
Use the following line in the file of the that folder which contains the file which is to be included sitewise.
define(‘ABSPATH’, dirname(__FILE__).’/');
The __FILE__ returns the full path and filename of the file.Now we are concerned with directory only which is accomplished by dirname() which returns directory name component of path.
Now let us suppose that settings.php file has to be included in many files of the site then you can use following statementto do this.
require_once(ABSPATH.’wp-settings.php’);


No comments:

Post a Comment