Jump to content

realtive path???


jonniejoejonson

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/197147-realtive-path/
Share on other sites

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

 

 

Link to comment
https://forums.phpfreaks.com/topic/197147-realtive-path/#findComment-1034852
Share on other sites

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 /

Link to comment
https://forums.phpfreaks.com/topic/197147-realtive-path/#findComment-1035262
Share on other sites

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.