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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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