Jump to content

Recommended Posts

So my site has a template, and each page plugs into it. But the pages are made dynamically, and some have directories, e.g. /directory/page, /directory2/page2, /directory2/sub-directory2/page3, etc.

If I want to include my template so the include function will display the template for every page, no matter how large the directory, how should it be coded?

I thought if I had

include '/template.php'

, then it would be the same as mydomain.com/template.php and work on every level, but this doesn't appear to be the case.

Edited by 1internet
Link to comment
https://forums.phpfreaks.com/topic/273282-directory-structure/
Share on other sites

include $_SERVER['DOCUMENT_ROOT'].'/template.php';

 

When dealing with file paths in PHP, '/' refers to the file-system root, not the root of your domain.  You can determine the root of your domain using the $_SERVER['DOCUMENT_ROOT'] variable.

Link to comment
https://forums.phpfreaks.com/topic/273282-directory-structure/#findComment-1406489
Share on other sites

Yes, it's definitely the standard way to include files. There are alternatives for different circumstances.

 

And yes, it will work on your local server. It'll work anywhere so long as the DOCUMENT_ROOT is defined (it practically always is) and the path to the file from there is "/template.php" (it's in that directory). If your local setup is different and you need a subfolder, like "/mydomain/template.php", then it will not work because the path is different.

Link to comment
https://forums.phpfreaks.com/topic/273282-directory-structure/#findComment-1406520
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.