Forum Moderators: coopster
//1. This include works from a file in my root dir
include ("../hidden_folder/connect_to_a_mysql_db.php");
//indicating I can use relative file names
//2. But this include from a file in my root dir doesn't work
include ("/functions/format_some_text.php");
//indicating I can't use relative file names
//3. Whereas this works
include($_SERVER["DOCUMENT_ROOT"]."/functions/format_some_text.php");
Can anyone explain why 2. does not work?
Thanks
Number 3 is an absolute and would be good in any file anywhere
Number 2 is actually neither. But if number 1 works for you, then changing number 2 to "functions/format_some_text.php" should work if the file with the include statement is located in the "functions" folder.
I want to include a file in a sub folder rahter than the same folder. I want to use a relative reference as it is easier to read.
What should the relative include statement look like if the file containing the include is /root/file_containing_include.php
and I want include file /root/functions/format_text.php
Thanks
Nubbin
include ('path/some.file'); // and
include ('./path/some.file'); always seemed the most reliable ways to do this on different server configs. document_root is a fairly reliable starting point. I often do..
$path = $_SERVER["DOCUMENT_ROOT"]; early on, which is handy, and easy to remember when you do..
include ($path.'/some/path/some.file'); later on. a cooler way to do it..
$root = $_SERVER['DOCUMENT_ROOT'];
$path = substr($_SERVER['PHP_SELF'],0,(strrpos($_SERVER['PHP_SELF'],'/')+1)); once these veriables are established (maybe in a config.php), it's real easy to remember something like..
include($root.$path.'some.file'); whenever you need a reliable path. this make things totally portable, too.
I'm fairly new to php myself, enjoying it, I've found this stuff useful, maybe some newbie browsing this thread would find useful too.
;o)
(or