Avago24
Avago24

Reputation: 33

PHP: include 'nav.php' with path to logo in other folder

So I have nav.php:

<div>
   <!-- and a lot of more code here so thats why i want write it once and include into all pages in main folder but also subfolders
   <img src="logo.png">
 </div>

And main folder looks like that:

In the index.php and all pages in main folder I did it like this:

<?php include 'nav.php'; ?>

In the secondpage.php in subfolder I did it this way:

<?php include '../nav.php'; ?>

But then logo don't appear because of course the path is wrong, it should be "../logo.png"

my solutions is:

  1. Make nav.php for subfolders in main folder with correct path
  2. Copy nav.php to every subfolder and link it like in main

but I don't think any of these solutions are acceptable if I have 10 subfolders?

Upvotes: 0

Views: 298

Answers (1)

wclark
wclark

Reputation: 436

Add a global variable $site_root. In your project initialization, set that to the root directory of your project. I suggest storing all images, like logos in a separate directory. ie: /img

Then you can create your links in any page like:

<img src="<?php echo $site_root; ?>/img/logo.png">

Upvotes: 1

Related Questions