1

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:\xampp\htdocs\root\Templates\topbar.php on line 2

Warning: include(): Failed opening '/Templates/header.php' for inclusion (include_path='C:\xampp\php\PEAR') in C:\xampp\htdocs\root\Templates\topbar.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.

6
  • 1
    can you show your folder structure? Without having a full visual, we might not able to help you. It seems to have a problem with your File accessing mechanism Commented Jul 3, 2020 at 15:02
  • Define a const, like, BASE_DIR = /your/root/path/ and use it for file inclusion Commented Jul 3, 2020 at 15:36
  • @SalimIbrogimov Thank you. I tried with define(ROOTH_PATH, “path), but it still doesn’t work or maybe I don’t get of how to use it properly. Commented Jul 3, 2020 at 16:04
  • You should use that const like this: include(ROOTH_PATH.'relative path'); And also add your entire file structure otherwise we wasting time playing in a guessing game. Commented Jul 3, 2020 at 16:06
  • @MonkeyD.Luffy Thank you, i’m sorry. I will do it, but I just thought that is not necessary as I wrote the connection between the folders. Commented Jul 3, 2020 at 16:06

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! It worked. I placed it like this: <link rel="stylesheet" href="http://ttransferhall.de/css/bootstrap.css" type="text/css" /> Is that the way I should do it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.