Jump to content

include inside a sub folder


Dilip

Recommended Posts

Hi, say I have inc folder inside root folder which has header.php. I add it to index.php as

 

include 'inc/header.php';

 

But say I have a folder name category inside the root folder and I have a file named category1.php, how to add the header.php into it?

If I use the above, it will look for a folder called inc in the same level as the category folder, right? How do I place header.php, nav.php and footer.php inside the inc folder and add them to sub folder or even deeper folders? Thanks.

 

Link to comment
Share on other sites

The path is relative to where it starts from. 

Without any qualification ("inc/header.php"), it starts from the current directory, i.e. where the file doing the including is. 

With a leading slash ("/inc/header.php"), it will start at the root of the site. 

You might also be able to navigate "upwards", e.g. "../inc/header.php", but that's actively barred in some systems and will drive you mad if you have to refactor the site significantly. 

Regards, 
   Phill W. 

 

  • Like 1
Link to comment
Share on other sites

Posted (edited)

Hi, I just tried using /inc/header.php in index.php as well as a subfolder PHP file. But it is not being called it all

Attaching the screenshot. Some of the content is not English

 

Screenshot from 2024-01-05 18-54-08.png

 

Edit 1:

the index.php works when I use `include "inc/header.php";` but not sure what to do for subfolders and deeper

Edited by Dilip
Link to comment
Share on other sites

6 hours ago, Phi11W said:

With a leading slash ("/inc/header.php"), it will start at the root of the site.

 

This is not quite accurate.  A leading slash indicates the root of the filesystem, not the root of the site. This is rarely what you want.

 

Link to comment
Share on other sites

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.