0

I have a header.php file, which contains files I want to use on different pages:

<link rel="stylesheet" href="css/nav.css" type="text/css">
<link rel="stylesheet" href="css/main.css" type="text/css">

I have the file inside a folder called resources which includes all my css files. I want to have the header so that it can directly link to the css files, but when I try to include the header in my webpages, it includes the path where the file that is including it is stored, not the relative path to where the header file is. How can I make it use the relative path for the header file, not the file where it is being included?

I've tried using, from other answers:

include( dirname(__FILE__) . 'css/main.css');

However, this gives me Not allowed to load local resource and gives the full file destination, but is correct

  • Possible duplicate of [PHP include relative path](https://stackoverflow.com/questions/17407664/php-include-relative-path) – Obsidian Age Jan 27 '19 at 21:43

1 Answers1

0

You should define the path to resources before including header.php:

define("res_path", "../resources/");
include(res_path."header.php");

Then in header care about that path:

<link rel="stylesheet" href="<?php echo res_path;?>css/nav.css" type="text/css">
<link rel="stylesheet" href="<?php echo res_path;?>css/main.css" type="text/css">

And in files that already have the correct path:

define("res_path", "resources/"); 
include(res_path."header.php");
Marco somefox
  • 370
  • 2
  • 10