Help Fast - Novice PHP Include Issue

Nolf-JobNolf-Job Inside each and every one of you!
edited September 2006 in Internet & Media
I'm new to php and am working on a site that will get moved over to a new server. I have my index.php file which I call include header.php and include footer.php. I can get it to work fine if I use absolute paths for the included files. I want to use relative paths from the web root and can't seem to figure out how. I've tried using include '/includes/header.php' but from what I've read that takes me to the file root of the server and not the web root.

When I try to use include($DOCUMENT_ROOT."/header.php") I get the following error:

Warning: include(includes/header.php) [function.include]: failed to open stream: No such file or directory in /home/champion/k/snolfi/www/epics/public/what_is_epics.php on line 2

Warning: include() [function.include]: Failed opening 'includes/header.php' for inclusion (include_path='.:') in /home/champion/k/snolfi/www/epics/public/what_is_epics.php on line 2

I'm testing it on a university account that is running php 5. Ideally, I just need to figure out how to do relative paths from the web root for all links as well. TIA

Comments

  • Nolf-JobNolf-Job Inside each and every one of you!
    edited September 2006
    So after looking over things a bit more, I guess the real issue is that I don't think I have access to any of the apache configuration files. Running phpinfo() tells me that the document root is opt/www/htdocs. Since the webspace I'm trying to host it from is a university account, I only have access to the directories under snolfi in /home/champion/k/snolfi/www/epics/public as shown above. Is it even possible to change the document root variable to something else through php?
  • ShortyShorty Manchester, UK Icrontian
    edited September 2006
    There two ways around this..

    Don't put all your files into the root directory. Instead work on creating an organised subfolder heirarchy.

    / (for your actual pages)
    /images (images directory)
    /wrappers (any odd functions and wrappers to other bits, classes)
    /logic (code files with no output)
    /templates (for your HTML templates)

    .. then you only to call (eg...)

    [php]
    require_once '/templates/header.php';
    [/php]

    or...

    Do the "siteroot" as a constant instead...

    [php]
    define("_MY_SITE_ROOT_", "/home/champion/k/snolfi/www/epics/public/", true);
    [/php]

    Just put that that the top of all your php files you create.

    Then you can use it like this..

    [php]
    require_once _MY_SITE_ROOT_.'/templates/header.php';
    [/php]
  • Nolf-JobNolf-Job Inside each and every one of you!
    edited September 2006
    Thanks Shorty, that helps with the first part.

    The other issue I'm having somewhat relates to this. My index.php file, as well as a lot of other files, include header.php in them. My directory structure is something like:

    /index.php
    /public/header.php
    /public/footer.php
    /page/class_path.php

    My header file includes /page/class_path.php but because I include the header.php in index.php, include statements made from header.php are referenced as if they're coming from index.php. I read that this issue can be resolved by using the statement

    include_once(realpath(dirname(__FILE__) . "/index.php"));

    and that is supposed to allow relative file including, but I think that calls upon absolute file names and so gives me a warning again for my site root as follows:

    Warning: include_once(../Page/class_path.php) [function.include-once]: failed to open stream: No such file or directory in /home/champion/k/snolfi/www/epics/public/header.php on line 2

    Warning: include_once() [function.include]: Failed opening '../Page/class_path.php' for inclusion (include_path='.:') in /home/champion/k/snolfi/www/epics/public/header.php on line 2

    Is there anything I can do to fix this, or is it because of an issue from trying to test this off of the university account directory?
  • ShortyShorty Manchester, UK Icrontian
    edited September 2006
    If you use the example constant that I gave you, you will not have this problem ;)

    For example

    [php]
    <?php
    define("_MY_SITE_ROOT_", "/home/champion/k/snolfi/www/epics/public/", true);

    require_once _MY_SITE_ROOT_.'page/class_path.php';

    require_once _MY_SITE_ROOT_.'public/header.php';

    echo 'Some html of some sort';


    require_once _MY_SITE_ROOT_.'public/footer.php';

    ?>
    [/php]
    :)
  • Nolf-JobNolf-Job Inside each and every one of you!
    edited September 2006
    I guess I should have been more clear. I'll end up moving this site over to another server that has a different root and don't want to have to re-edit all the files if I can avoid it.

    What I ended up doing was creating a php class file that contains all of the paths for any external files relative to the root directory. Then a variable that allows the root directory to be changed depending on where the site is located. It seems to be working just fine.
  • ShortyShorty Manchester, UK Icrontian
    edited September 2006
    I personally wouldnt use a variable. No reason not to make it a constant. Consider a constant as a one time set variable and cannot be adjusted ;)
  • Nolf-JobNolf-Job Inside each and every one of you!
    edited September 2006
    Good point. I'll change that.

    I have a new question in that I have an external css document that I can't use the following code to import
    [PHP]<style type="text/css">@import&quot;<?php $path->getPath("style_sheet"); ?>";</style>[/PHP]
    because my getPath function operates on the file directory level instead of just the web directory level, so it tries to import something like "home/local/snolfi...." and doesn't find that within the web directory.

    Is there anyway that I can either write a function to list the directory from within the website only or somehow put my css into a php file so that it can be imported directly.

    Right now I just have it being included in the head of the html document and I'd prefer not doing that.
Sign In or Register to comment.