Absolute path and Relative path file inclusion in PHP

Posted on January 31, 2008 
Filed Under php, tips and technique

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__ and dirname() 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(’ABSLPATH’, 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’);

Popularity: 17% [?]

Enter your email address and get recent tutorials, tips, tricks and scripts of PHP, Ajax, JavaScript and CSS directly delivered to you email inbox:

Follow me on twitter at http://twitter.com/roshanbh.

Related Posts

» Getting filename and extension in PHP using explode() ,basename() and pathinfo()
» 8 useful server variables available in PHP
» Force download multiple files in a zip archive using PHP
» Blink and bounce effect on image or object using jquery

Comments

7 Responses to “Absolute path and Relative path file inclusion in PHP”

  1. PHP Coding School » Blog Archive » php tips [2008-01-31 19:06:43] on January 31st, 2008 7:20 pm

    [...] Absolute path and Relative path file inclusion in PHP By Roshan First let me explain you about absolute path and relative path in the server with the example using include() function of PHP. include(”/home/example/public_html/config.php”); //absolute path include(”config.php”); //relative path. - http://roshanbh.com.np [...]

  2. Pages tagged "absolute" on February 3rd, 2008 7:15 pm

    [...] bookmarks tagged absolute Absolute path and Relative path file inclusion in … saved by 8 others     Nathantheloser bookmarked on 02/03/08 | [...]

  3. tux86 on February 4th, 2008 4:30 pm

    thanks.

  4. Ben Parsonson on March 18th, 2008 3:38 am

    Thanks so much for this article - it’s helped me a great deal with inlcuding sensitive data (database user/passwords for example) without the data being available to the public.

  5. Roshan on March 18th, 2008 5:23 am

    Thanks ben…

  6. Sander on July 15th, 2008 4:09 pm

    Thanks, just what I was looking for! I knew about the difference between the two types (of course, I almost wanted to write). But I was struggling with the issue of a remote and local server. But this solves it completely!

    Regards from the Netherlands,
    Sander

  7. Roshan on July 15th, 2008 4:40 pm

    @Sander - Welcome dude!!.. It’s so nice to hear that this post was useful for you.

    Greetings from Nepal

Leave a Reply