Reputation: 33
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:
nav.php
logo.png
index.php
subfolder with secondpage.php
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:
nav.php
for subfolders
in main folder with correct pathnav.php
to every subfolder and link it like in mainbut I don't think any of these solutions are acceptable if I have 10 subfolders?
Upvotes: 0
Views: 298
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