Absolute path and Relative path file inclusion in PHP

Advertisement

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(‘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’);

Popularity: 17% [?]

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

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

25 Comments on “Absolute path and Relative path file inclusion in PHP”

  • tux86 wrote on 4 February, 2008, 16:30

    thanks.

  • Ben Parsonson wrote on 18 March, 2008, 3:38

    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.

  • Roshan wrote on 18 March, 2008, 5:23

    Thanks ben…

  • Sander wrote on 15 July, 2008, 16:09

    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

  • Roshan wrote on 15 July, 2008, 16:40

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

    Greetings from Nepal

  • Wayne wrote on 13 October, 2008, 13:25

    Thank a million for this!

  • Niki wrote on 16 November, 2008, 13:15

    Thank you,
    this was right what I was looking for!

    Greetings from Italy :)

  • Attila wrote on 24 November, 2008, 8:56

    thanks a lot, it was very helpfullllll :)

  • Shyam wrote on 14 January, 2009, 8:03

    Thanks dude!!!!

  • Jonathan Ellse wrote on 26 April, 2009, 18:43

    Brilliant article, but you put “ABSLPATH” the first time then “ABSPATH” without the L.

    Easy to miss for Copy + Paste

    Jonathan

  • Michael wrote on 25 May, 2009, 9:37

    I still like the idea of relative paths. tag in HTML helps a lot. That with all relative paths are relative to the base-path.
    You’ll just have to script in the URI of the site’s current location.

  • Marcus wrote on 29 May, 2009, 12:30

    Thanks! This was very helpful for me!

    Greetings from Germany

  • Vernon wrote on 1 June, 2009, 14:08

    Thanks so much. It was very helpful. It created new problems, but that’s the way it goes.

  • Mike wrote on 28 June, 2009, 23:14

    Thanks a lot! This helped me with the deployment problems I was having with my first PHP application.

  • Abhishek wrote on 14 July, 2009, 13:50

    Thanks mate!!!

  • Costa Rica wrote on 9 September, 2009, 23:47

    Roshan, I have this situation:

    I’m creating a new version of a website, on my test server, the subdomains are subfolder of the root and I’ don’t have problems with Absolute paths, but in the liver server, the subdomains are in diferentes folder than the root folder, so they are not working, just the absolute icludes for their respective subfolders, any idea if this problem is bcz of the pacahe version or server permissions?

    By the way, with the variable $_SERVER['DOCUMENT_ROOT']; you can get the absolute path too.

  • mazoku wrote on 12 October, 2009, 13:15

    Thanks for the advice! Very useful and basic, indeed.

  • imin wrote on 12 November, 2009, 8:52

    cool! this is what i’ve been looking for. thanks a lot!

  • andy wrote on 10 June, 2010, 13:36

    awesome! just what I was looking for, thanks!

  • Gran Canaria wrote on 28 June, 2010, 12:11

    Thanks man, thats is great

  • Sandeep wrote on 14 July, 2010, 10:14

    Thanks for the Information.

    Hey Roshan where are you man? You are not updating the Blog since last year.

    We are missing you dude.

    @sunware

  • Reactorx2 wrote on 5 February, 2011, 19:15

    Thank you

  • Prashant wrote on 24 August, 2011, 17:52

    thanks

Trackbacks

  1. PHP Coding School » Blog Archive » php tips [2008-01-31 19:06:43]
  2. Pages tagged "absolute"

Write a Comment

 


Copyright © 2013 Roshan Bhattarai's Blog. All rights reserved.
Powered by WordPress.org, Custom Theme and ComFi.com Calling Card Company.