Michdd Posted July 20, 2009 Share Posted July 20, 2009 This might be kind of a stupid question.. How can I make a path so it'll work even when including the file from another directory? I have a template class that gets it's files like template/html/(FILENAME).html . I have a structure like this: root/common.php <- Includes all nessary files, including class/template.class.php root/class/template.class.php root/anyfile.php <-- This includes common.php, and can use the template class, no problems root/network/network.php <- has (require_once '../common.php' I get an error here because the path used in the class/template.class.php is incorrect in relation to this file.. So basically I need to make the path work so it'll change to be relevant to the file from which it's being included.. Quote Link to comment https://forums.phpfreaks.com/topic/166649-solved-relevant-paths/ Share on other sites More sharing options...
wildteen88 Posted July 20, 2009 Share Posted July 20, 2009 What I'd do is define a constant called ROOT which contains the full path to my sites root folder. So add this file to common.php define('ROOT', $_SERVER['DOCUMENT_ROOT'].'/'); Now include common.php as you normally would. But when including your other files such as template.class.php for example you'd use include ROOT.'class/template.class.php'; This will sort out your path issues. Quote Link to comment https://forums.phpfreaks.com/topic/166649-solved-relevant-paths/#findComment-878772 Share on other sites More sharing options...
rhodesa Posted July 20, 2009 Share Posted July 20, 2009 in common.php use: require_once(dirname(__FILE__).'/class/template.class.php'); __FILE__ represents the current file where the variable resides. so even if it is included from a file in a different folder, it remains constant Quote Link to comment https://forums.phpfreaks.com/topic/166649-solved-relevant-paths/#findComment-878773 Share on other sites More sharing options...
Michdd Posted July 20, 2009 Author Share Posted July 20, 2009 in common.php use: require_once(dirname(__FILE__).'/class/template.class.php'); __FILE__ represents the current file where the variable resides. so even if it is included from a file in a different folder, it remains constant That doesn't solve my problem. I get the same results using that. I think I can probably use something like in my template class to include the template files. But I must be doing it wrong. It looks like this: file_get_contents('template/html/main.html') I think I have to use __FILE__ to access the path(root/template/html/main.html) from this file(root/class/template.class.php) Quote Link to comment https://forums.phpfreaks.com/topic/166649-solved-relevant-paths/#findComment-878785 Share on other sites More sharing options...
Michdd Posted July 20, 2009 Author Share Posted July 20, 2009 Solved. Solution: file_get_contents(dirname(dirname(__FILE__)) . '/template/html/main.html') Quote Link to comment https://forums.phpfreaks.com/topic/166649-solved-relevant-paths/#findComment-878795 Share on other sites More sharing options...
rhodesa Posted July 20, 2009 Share Posted July 20, 2009 yup...that is how you do it. and if you are going to be using it a lot, it's probably easier to define a variable or a constant in common.php that everything else can use...similar to how @wildteen88 did it. but i would still use dirname(__FILE__) over $_SERVER['DOCUMENT_ROOT'], as $_SERVER['DOCUMENT_ROOT'] can change depending on how you have your webserver stack configured...while dirname(__FILE__) will always be the same Quote Link to comment https://forums.phpfreaks.com/topic/166649-solved-relevant-paths/#findComment-878802 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.