wobuck Posted January 29, 2011 Share Posted January 29, 2011 I am trying to include a inc.php file within a normal .php file from a single directory as I am using it in several directories but cannot get it to work and have tried many different techniques. Any ideas on the coding that would work would be appreciated? Alternatively, I would be happy to set the constants in a mysql db but am unsure how I would unpack/read it so as to get it in the php program as code i.e. mysql db field 1 field 2 name $name Program <?php $name; ?> Link to comment https://forums.phpfreaks.com/topic/226056-include-constantsdatafileincphp-from-any-directory/ Share on other sites More sharing options...
l4nc3r Posted January 29, 2011 Share Posted January 29, 2011 What's wrong with include('inc.php'); ? Link to comment https://forums.phpfreaks.com/topic/226056-include-constantsdatafileincphp-from-any-directory/#findComment-1166984 Share on other sites More sharing options...
wobuck Posted January 29, 2011 Author Share Posted January 29, 2011 Your Question "What's wrong with include('inc.php'); ?" include("constantsdata.inc.php"); this works ok but the file,constantsdata.inc.php, has to be in the same directory as the php file calling the include which means I have to maintain every copy of this file in all directories I am using it. I have tried variants like: include("http://localhost/folder/constantsdata.inc.php"); include('http://localhost/folder/constantsdata.inc.php'); to call the file from another directory e.g. folder but it does not work? Link to comment https://forums.phpfreaks.com/topic/226056-include-constantsdatafileincphp-from-any-directory/#findComment-1167042 Share on other sites More sharing options...
BlueSkyIS Posted January 29, 2011 Share Posted January 29, 2011 do not include a URL to a website, include a path to the file on the computer. include('/path/to/webroot/folder/constantsdata.inc.php'); Link to comment https://forums.phpfreaks.com/topic/226056-include-constantsdatafileincphp-from-any-directory/#findComment-1167091 Share on other sites More sharing options...
wobuck Posted January 30, 2011 Author Share Posted January 30, 2011 Using BlueSkyIS's response set me on the right 'path' to resolving the issue: Used this to find paths: print realpath(basename(getenv("SCRIPT_NAME"))); print("<br>"); print realpath(basename(getenv("DOCUMENT ROOT"))); print("<br>"); Resulting in working out this code which works: $docRoot = getenv("DOCUMENT_ROOT"); include $docRoot."/folder/constantsdata.inc.php"; Many Thanks to BlueSkyis! Link to comment https://forums.phpfreaks.com/topic/226056-include-constantsdatafileincphp-from-any-directory/#findComment-1167233 Share on other sites More sharing options...
ngreenwood6 Posted January 30, 2011 Share Posted January 30, 2011 you could just use $_SERVER['DOCUMENT_ROOT'] as well. The $_SERVER array holds alot of useful data. You can find what it holds by doing print_r($_SERVER). Link to comment https://forums.phpfreaks.com/topic/226056-include-constantsdatafileincphp-from-any-directory/#findComment-1167235 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.