Can I alter paths to work in both a folder and a subfolder?

Discussion in 'PHP' started by egdcltd, Aug 12, 2007.

  1. #1
    I have a folder, blacknova, which has in it a header and a footer file. I have made a sub-folder, admin, and I'm calling the header and footer files like this:

    include("../header.php");
    
    include("../footer.php");
    PHP:
    In general, this works. However, the are some things (eg images) in the header and footer files that aren't being called because the path, although right for the blacknova folder, sin't right for the admin folder, eg

    images/bgoutspace1.gif
    Code (markup):
    Is there any way of altering the paths so that they will be correct for both blacknova and blacknova/admin folders, or will it be necessary to make new header and footer files for the admin folder?
     
    egdcltd, Aug 12, 2007 IP
  2. killerj

    killerj Active Member

    Messages:
    765
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    95
    #2
    I'm not sure if your server would support it but try using absolute web address
    eg :
    
    include("http://yourdomian.com/blah.php");
    
    PHP:
    if it works, well and good for you :)
     
    killerj, Aug 12, 2007 IP
  3. l0gic

    l0gic Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The working directory of the PHP script isn't necessarily the web root. For files referenced by an end user's browser (images, stylesheets, scripts, etc.), always make them relative to the web root:
    <img src="/images/foo.jpg" />
    Code (markup):
    Referencing included files from the web root (the first slash) ensures the browser knows where they are, regardless of where the page being viewed resides.
     
    l0gic, Aug 12, 2007 IP
  4. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'd prefer not to use absolute web addresses; I may distribute the script and I'd rather the locations weren't pointing to my server.

    Regarding making them relative to the web root, that probably also won't be good for working on other servers; also, it doesn't seem to work for the admin sub-folder.
     
    egdcltd, Aug 12, 2007 IP
  5. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    The easiest thing would be to add this to the header file's <head> section
    <base href="http://www.yoursite.com" />
    HTML:
    Obviously that's useless when you're redistributing your code to other sites, so you could use
    <base href="http://<?=$_SERVER["HTTP_HOST"]?>" />
    HTML:
    Failing that, you could always set a path variable to prepend when necessary, e.g. in the admin files, do this
    
    <?
    $strPath = "../";
    include("../header.php");
    
    include("../footer.php");
    ?>
    PHP:
    and in the header.php do this
    <img src="<?=$strPath?>images/bgoutspace1.gif"
    HTML:
    Strictly, you should set $strPath = "" in your non-admin pages, but it will work fine without it as long as notices aren't shown.
     
    void, Aug 12, 2007 IP
  6. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks, that looks like the type of thing I'm after. I'll give it a try.
     
    egdcltd, Aug 16, 2007 IP
  7. andrew47

    andrew47 Active Member

    Messages:
    319
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #7
    l0gic is right.
    So there are two types of path - php & html. For html link use the ultimate path i.e. src="/images/image.gif" (so it starts with "/" and has a full path to the image.

    When it comes to php includes user the following
    include $_SERVER['DOCUMENT_ROOT']."/includes/file.php";
    that will give u an ultimate path for php files
     
    andrew47, Aug 16, 2007 IP