jonniejoejonson Posted March 31, 2010 Share Posted March 31, 2010 There are many applications like wordpress etc that let you create themes or templates... Say the theme is created by a file called myThemeFile.php file in the following dir.... includes/templates/classicTheme/myThemeFile.php What i dont understand is that within this myThemeFile.php you can include other files relative to its file path... not the root file path... so how does this work? i could understand if you were opening the file from within myThemeFile.php dir, however the myThemeFile.php is being included or added from an index.php file that is in the root directory... therefore all the includes statements that are in myThemeFile.php should be relative to the root dir. I hope you understand what i mean... im not sure i do!... kind regards J Quote Link to comment https://forums.phpfreaks.com/topic/197147-realtive-path/ Share on other sites More sharing options...
andrewgauger Posted March 31, 2010 Share Posted March 31, 2010 __DIR__ The directory of the file. If used inside an include, the directory of the included file is returned. This is equivalent to dirname(__FILE__). This directory name does not have a trailing slash unless it is the root directory. (Added in PHP 5.3.0.) http://php.net/manual/en/language.constants.predefined.php Hope this is what you are looking for: include_once(__DIR__."/filename.php"); Your going to want to check the syntax of __DIR__ to see if it trails with a "/" or not. Quote Link to comment https://forums.phpfreaks.com/topic/197147-realtive-path/#findComment-1034852 Share on other sites More sharing options...
ignace Posted March 31, 2010 Share Posted March 31, 2010 I believe __DIR__ is only available since PHP 5.3.x and more then likely he has a version <5.3 @OP DIRECTORY_SEPARATOR solves the slash thing include_once(rtrim(dirname(__FILE__), '/\\').DIRECTORY_SEPARATOR.'filename.php'); Quote Link to comment https://forums.phpfreaks.com/topic/197147-realtive-path/#findComment-1034904 Share on other sites More sharing options...
jonniejoejonson Posted April 1, 2010 Author Share Posted April 1, 2010 Thanks Guys... require_once( str_replace('//','/',dirname(__FILE__).'/') .'dir/hello.php'); prefect. Quote Link to comment https://forums.phpfreaks.com/topic/197147-realtive-path/#findComment-1035227 Share on other sites More sharing options...
ignace Posted April 1, 2010 Share Posted April 1, 2010 PHP has a special constant that will hold the OS specific directory separator called DIRECTORY_SEPARATOR when you call dirname(__FILE__) you will already have the correct OS directory separator so no need to use str_replace() on it. However I add rtrim() to remove any trailing slashes both \\ as / Quote Link to comment https://forums.phpfreaks.com/topic/197147-realtive-path/#findComment-1035262 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.