Dilip Posted January 5, 2024 Share Posted January 5, 2024 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. Quote Link to comment https://forums.phpfreaks.com/topic/317601-include-inside-a-sub-folder/ Share on other sites More sharing options...
Phi11W Posted January 5, 2024 Share Posted January 5, 2024 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/317601-include-inside-a-sub-folder/#findComment-1613944 Share on other sites More sharing options...
Dilip Posted January 5, 2024 Author Share Posted January 5, 2024 (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 Edit 1: the index.php works when I use `include "inc/header.php";` but not sure what to do for subfolders and deeper Edited January 5, 2024 by Dilip Quote Link to comment https://forums.phpfreaks.com/topic/317601-include-inside-a-sub-folder/#findComment-1613945 Share on other sites More sharing options...
Barand Posted January 5, 2024 Share Posted January 5, 2024 Are you sure "index.php" is in your root folder? Quote Link to comment https://forums.phpfreaks.com/topic/317601-include-inside-a-sub-folder/#findComment-1613947 Share on other sites More sharing options...
Dilip Posted January 5, 2024 Author Share Posted January 5, 2024 (edited) Yes, please see Edit 1 : It works fine when I use include " ../inc/header.php"; Edited January 5, 2024 by Dilip Quote Link to comment https://forums.phpfreaks.com/topic/317601-include-inside-a-sub-folder/#findComment-1613949 Share on other sites More sharing options...
kicken Posted January 5, 2024 Share Posted January 5, 2024 On 1/5/2024 at 11:26 AM, Phi11W said: With a leading slash ("/inc/header.php"), it will start at the root of the site. Expand 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. Quote Link to comment https://forums.phpfreaks.com/topic/317601-include-inside-a-sub-folder/#findComment-1613955 Share on other sites More sharing options...
Dilip Posted January 6, 2024 Author Share Posted January 6, 2024 I guess ../ is the only way then . Quote Link to comment https://forums.phpfreaks.com/topic/317601-include-inside-a-sub-folder/#findComment-1613965 Share on other sites More sharing options...
maxxd Posted January 6, 2024 Share Posted January 6, 2024 $_SERVER['DOCUMENT_ROOT'].'/inc/header.php'; This will work from any sub-directory, as long as the `inc` directory is at the top level of the file hierarchy. 1 Quote Link to comment https://forums.phpfreaks.com/topic/317601-include-inside-a-sub-folder/#findComment-1613976 Share on other sites More sharing options...
Dilip Posted January 7, 2024 Author Share Posted January 7, 2024 Thanks @maxxd Quote Link to comment https://forums.phpfreaks.com/topic/317601-include-inside-a-sub-folder/#findComment-1613989 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.