how do I get file1's name from file2?

Discussion in 'PHP' started by zeebloyankees, Aug 14, 2006.

  1. #1
    file1.php

    require_once(file2.php);
    $object = new class2();
    PHP:
    file2.php

    class class2
    {
    
    }
    PHP:
    If I include file 2 from file 1, how do I get file1's name from file2?
     
    zeebloyankees, Aug 14, 2006 IP
  2. ip076

    ip076 Peon

    Messages:
    79
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm really unclear as to what you are trying to do, but you could try $_SERVER['PHP_SELF']

    If you include that variable in your class in file2, and then call it as an include in file1, I think it will return file1...
     
    ip076, Aug 14, 2006 IP
  3. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #3
    $_SERVER['PHP_SELF'] returns the location of the executed script. If you execute domain.com/dir/test.php $_SERVER['PHP_SELF'] will return /dir/test.php
     
    smatts9, Aug 14, 2006 IP
  4. ip076

    ip076 Peon

    Messages:
    79
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4

    So, if class2 in file2.php used the variable $_SERVER['PHP_SELF'], and he called that class from file1.php, it will return the string location of file1.php, which he could then use some string functions on in order to get the filename....right??? I could be wrong....
     
    ip076, Aug 14, 2006 IP
  5. TwistMyArm

    TwistMyArm Peon

    Messages:
    931
    Likes Received:
    44
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It would really help if you explain why you're trying to do this... there may be an easier way!

    If worse comes to worst, you could always use debug_backtrace ( http://ca.php.net/manual/en/function.debug-backtrace.php ) and then track back through the array to determine the parent file.
     
    TwistMyArm, Aug 15, 2006 IP
  6. ThomasNederman

    ThomasNederman Peon

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #6
    if you try

    $file = basename($_SERVER['PHP_SELF']);

    Not tried it but i would say it should give a correct reply
     
    ThomasNederman, Aug 15, 2006 IP
  7. ip076

    ip076 Peon

    Messages:
    79
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    So, here is what I tried for you:

    page1.php:

    
     
    include("page2.php");
     
    
    PHP:
    page2.php:

    
     
    echo basename($_SERVER['PHP_SELF']);
     
    
    PHP:
    when you load page1.php it displays page1.php.

    Hopefully this helps, but like previously mentioned, if you could possibly explain how you're using it and what you're trying to do, we might be able to give a better answer.
     
    ip076, Aug 15, 2006 IP
  8. zeebloyankees

    zeebloyankees Guest

    Messages:
    32
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Its ok I got all the information I needed from you guys. Thanks for the great advice
     
    zeebloyankees, Aug 16, 2006 IP