Help Fast - Novice PHP Include Issue
Nolf-Job
Inside each and every one of you!
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
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
0
Comments
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]
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?
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]
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.
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"<?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.