Jump to content

[SOLVED] relevant paths.


Michdd

Recommended Posts

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..

Link to comment
https://forums.phpfreaks.com/topic/166649-solved-relevant-paths/
Share on other sites

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.

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)

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.