SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,801
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)

    get the 'URL' of an INCLUDE file.

    Is there a way for an include to store its own URL?
    Using something like 'PHP_SELF','SCRIPT_NAME','REQUEST_URI' will return info pertaining to the file that includes the script ( of course) which is not what I want in this case.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,151
    Mentioned
    16 Post(s)
    Tagged
    3 Thread(s)
    I believe the __FILE__ magical constant is what you are after. Though that is the files full path not a URL. If you must have a URL you could do some string manipulation to get only the part accessible via http.
    The only code I hate more than my own is everyone else's.

  3. #3
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,801
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    oodz,

    Actually __FILE__ was my second attempt ( I should have mentioned it, but i didnt know how to elaborate on it).

    What i am trying to accomplish is figure out where the ROOT of the SITE installation is. For example: the index page for the site could be directly at mysite.com or mysite.com/guests/sites/myActualsite/

    so i need a way to make a url from file, even if i dont know where my initial directory actually is

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I am a wee bit confused by the question because include files are normally stored outside (ie above) the site's doc root for security reasons.

    Maybe you could work out the path you need by looking at all the vars output from one of you scripts:

    PHP Code:
    var_dump($_SERVER); 
    Perhaps explaining further about why you need to grab the url of an included file would shine a light on the exact problem you face?

  5. #5
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,801
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    No worries it is a bit confusing.

    This is what I have com up with thus far, maybe it will show what i am trying to do ( actually it works!! ) but maybe there is a more graceful way to do this?


    Code:
    	$prebase=explode("/",__FILE__);
    	$pbat=isset($prebase[count($prebase)-4])? '/'.$prebase[count($prebase)-4]:'';
    	$pb2='http://'.$_SERVER['SERVER_NAME'].$pbat;
            declare ('ROOT', $pb2);

  6. #6
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    <?php
        $prebase
    =explode("/",__FILE__);
        
    $pbat=isset($prebase[count($prebase)-4])? '/'.$prebase[count($prebase)-4]:'';
        
    $pb2='http://'.$_SERVER['SERVER_NAME'].$pbat;
            
    define ('ROOT'$pb2);
    ?>
    Using define works for me.

    PHP Code:
    <?php
    include "dresden_test.php";

    echo 
    "This file is at "__FILE__ ;
    echo 
    "<hr /> ";
    echo 
    "The root is at " ROOT ;

    var_dump$prebase );
    Gives:
    Code:
    This file is at C:\var\www\html\code\phoenix_test.php
    The root is at http://code
    array
      0 => string 'C:\var\www\includes\dresden_test.php' (length=36)
    EDIT, and yes http://code is the domain name for where I test stuff on localhost.

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I recommend just hard coding this stuff, easier that way then trying to be clever. The application should if ever change locations (relative to the web server's document root) and when it does it would be easy to adjust.

    Btw... $_SERVER['DOCUMENT_ROOT'] str_replace( $_SERVER['DOCUMENT_ROOT'], __DIR__, $rel_path );
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    SitePoint Member blogaddition's Avatar
    Join Date
    Sep 2012
    Location
    India
    Posts
    23
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Just use

    <?php
    echo $_SERVER['SERVER_NAME']. $_SERVER['PHP_SELF'];
    ?>

    in your code that will give you the exact url of your 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
  •