SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    what is __DIR__ ?

    Sometimes when I look at other peoples PHP code they have something like this in there:

    include(__DIR__"file.php");

    what is __DIR__?

    Thanks.

  2. #2
    SitePoint Zealot
    Join Date
    Dec 2007
    Location
    Mackay, QLD, Australia
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only one I know about is __FILE__, it is the full name and path of the current file.

    The only way I can see that they get __DIR__ is by defining it like this

    PHP Code:
    define('__DIR__'pathinfo(__FILE__PATHINFO_DIRNAME)); 
    Which will give you the absolute path to the current file.

  3. #3
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorry yes i meant __FILE__

    I dont quite get it - this refers to the filename of the PHP script or something else?

  4. #4
    SitePoint Zealot
    Join Date
    Dec 2007
    Location
    Mackay, QLD, Australia
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It refers to the absolute path and filename of the script that calls it. So, if I created a test script on my local machine, and say
    PHP Code:
    echo __FILE__
    I would get this
    C:\Apache2\htdocs\test.php

    If, say for example I had 2 scripts, called test1.php and test2.php, and I included test2.php in test1.php like this

    test1.php
    PHP Code:
    <?php
    include('test2.php');
    echo 
    __FILE__;
    ?>
    test2.php
    PHP Code:
    <?php
    echo __FILE__.'<br />';
    ?>
    I would get this output
    C:\Apache2\htdocs\test2.php
    C:\Apache2\htdocs\test1.php

    So it will always show the full path and filename of that script.

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok i see thanks. So its basically the same as $_SERVER['php_self'] except it shows the full pathname on the server.

  6. #6
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also use this for the same purpose:
    PHP Code:
    echo $_SERVER['SCRIPT_FILENAME']; 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2007
    Location
    Mackay, QLD, Australia
    Posts
    158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mameha View Post
    ok i see thanks. So its basically the same as $_SERVER['php_self'] except it shows the full pathname on the server.
    Yup, exactly right

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2006
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks, that is one mystery I can tick off the list

  9. #9
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not exactly like PHP_SELF. That will always point to the file requested by the browser. If you want to know the path to an include, not to the file that includes it, you would use __FILE__.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •