Get "includee" file?

Discussion in 'PHP' started by Triexa, May 20, 2007.

  1. #1
    Is there some way to get the name of the file that called the current one, either through a require() or include()?
     
    Triexa, May 20, 2007 IP
  2. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I don't know of a way to do that, but if there is only one level of includes, $_SERVER['SCRIPT_FILENAME'] will tell you what you need to know.
     
    lemaitre, May 20, 2007 IP
  3. Triexa

    Triexa Active Member

    Messages:
    580
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #3
    That isn't available when run from the command line..
     
    Triexa, May 20, 2007 IP
  4. Servii

    Servii Banned

    Messages:
    55
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    <?php include("replacefilehere.php"); ?>

    thnx
    travis
     
    Servii, May 20, 2007 IP
  5. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #5
    echo __FILE__;
    PHP:
     
    gibex, May 20, 2007 IP
  6. DeViAnThans3

    DeViAnThans3 Peon

    Messages:
    785
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    0
    #6
    That will return the file in which the code is, not the filename of the parent file I think :)

    So far I know if, there is no possibility to check it, unless if you put in the parent file the following.
    $parent_file = "parentfile.php"; // The current file
    include("childfile.php"); // The include, so you must put the above code above the include() or require()
    PHP:
    In the child file you can then call the parent filename with using the variable $parent_file. (The variable $parent_file doesn't has to be made global or so, it will automaticly pass).

    Hope this helps.
     
    DeViAnThans3, May 20, 2007 IP
  7. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I would probably just set a variable as suggested (well, I would actually question the architecture, but anyway...).

    If you REALLY want it to be dynamic, you could look at the output of the debug_backtrace function and traceback until you find the parent file.
     
    TwistMyArm, May 20, 2007 IP
  8. Triexa

    Triexa Active Member

    Messages:
    580
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #8
    Hmm alright well I'll just take the variable route... Was just wondering. Thanks to everyone
     
    Triexa, May 20, 2007 IP
  9. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #9
    I've done something like this before.

    In the parent file, put:
    $currentFile = $_SERVER["PHP_SELF"];
    $parts = Explode('/', $currentFile);
    $parentfile = $parts[count($parts) - 1];
    
    PHP:
    And in the included file:
    echo $parentfile;
    PHP:
    Should work.
     
    dp-user-1, May 20, 2007 IP
  10. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #10
    NinjaNoodles: that would only work for one level of inclusion, though. If FileA included FileB and FileB included FileC, FileC would still see 'FileA'. I guess that may be what the OP wanted but if that were they case, there's no real need for anything special, anyway, as PHP_SELF is available in FileC in the first place, right?
     
    TwistMyArm, May 21, 2007 IP
  11. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #11
    Well, PHP_SELF gets the file path, whereas the OP just wanted the file name, no?
     
    dp-user-1, May 21, 2007 IP
  12. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Like I said, it would only work for one level of inclusion as PHP_SELF relates to the script that is being executed. If you read my example, you would see that even if you had 1 file including another and that file including another file, that last file would still see the initial 'root' file, not the file that is including it.
     
    TwistMyArm, May 21, 2007 IP
  13. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #13
    Well, I don't have the time to code it ATM, but there's probably a way to dynamically generate variables like $parentfile2 if ( isset($parentfile) ), and $parentfile3, and so on...
     
    dp-user-1, May 21, 2007 IP
  14. Triexa

    Triexa Active Member

    Messages:
    580
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #14
    also, basename($_SERVER['PHP_SELF']) is quicker/cleaner than your method :)
     
    Triexa, May 21, 2007 IP
  15. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Oh well. If you really wanted you could take my suggestion of using the output of debug_backtrace within a function and it would be pretty simple to do completely dynamically. I don't have time to do the code but it would be pretty easy.
     
    TwistMyArm, May 21, 2007 IP