Skip to content
Advertisement

Include PHP file form Subfolder

I’m very sorry for asking such stupid questions which there are answers here, but I just don’t get, and I’m stuck into it.

So, I will try to explain simply, what did I do. I’m trying to make a simple cooperative website for personal use and the project is on localhost. I use a ready template with HTML code, and I break it apart to topbar.php, home-content.php, footer.php, and I placed it in another folder called Templates.

Everything seems to work fine from the root folder, but when I try to made a subfolder – calculator and made an index.php with:

<?php
include("../Templates/topbar.php");
include("../Templates/footer.php");

then the HTML loads, but all links tags seem to not work. The content is loaded, but without the CSS, JS, and others.

I read about it and tried with:

$_SERVER['DOCUMENT_ROOT']."/Templates/topbar.php

It doesn’t seem to work.

Also into the topbar.php file, there is another include, which is include "header.php" with the <head> part of the HTML.

When I try to get into

root/calculator

It shows me this error:

Warning: include(/Templates/header.php): failed to open stream: No such file or directory in C:xampphtdocsrootTemplatestopbar.php on line 2

Warning: include(): Failed opening ‘/Templates/header.php’ for inclusion (include_path=’C:xamppphpPEAR’) in C:xampphtdocsrootTemplatestopbar.php on line 2

There are many answers to this subject, but can someone explain to me how can I deal with this like to a really silly guy…

Thank you all.

Advertisement

Answer

The PHP files are getting included that is not a problem.

Problem is in the HTML paths.

When you see the output on that broken page. View Source of that page (CTRL + U on most systems)

The stylesheet and the images loading are not relative to the current path.

So to overcome this problem just use full paths for images and stylesheet.

You can still use relative paths for inner pages too but let’s not complicate it right now.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement