0

I am writing a website and I decided to separate my header into it's own php file and then use the include statement to insert it back into my webpage. The issue I'm having is on my index.php I'm sourcing my logo as such:

<img class='logo' src='images/logo.png'>

and on my meats/sausage.php page I source it as such:

<img class='logo' src='../images/logo.png'>

Is there a superglobal or something I can use instead to direct both pages to the correct location or do I need to make separate header versions for each of the 2 pages?

Note: I'm using XAMPP and localhost rather then running it on the web just yet.

Alec Davies
  • 127
  • 11

4 Answers4

1

Use a constant or something that defines your base directory and work with that.

For example:

<?php

$baseDirectory = __DIR__;

function assets_path($append) 
{
    return "{$baseDirectory}{$append}";
}

And then:

<img class="logo" src="<?= assets_path('images/logo.png') ?>">

Choosing where to place this is on you.

GiamPy
  • 3,543
  • 3
  • 30
  • 51
0

A possible option would be to use .htaccess file and the RewriteRule like so:

RewriteRule ^(.*)SiteAssets/(.*)$ http://YourSite.com/AssetsRoot/$2

And then use like this:

<img class="logo" src="SiteAssets/images/logo.png">

Also, if your DocumentRoot setting is correctly set in httpd.conf, you could simply use this throughout your site:

<img class="logo" src="/images/logo.png">

See https://stackoverflow.com/a/7823598/715105

JustBaron
  • 2,319
  • 7
  • 25
  • 37
0

Base tag

<head>
  <base href="https://example.com/">
</head>

This will set your base on every page. No need to use ../images

see https://www.w3schools.com/tags/tag_base.asp

Maxwell s.c
  • 1,583
  • 15
  • 29
0

Duplicate of this question:

src absolute path problem

The trick was to reference it as such:

 <img src="http:\\localhost\site\img\mypicture.jpg"/>

but with forward slashes rather then backward slashes

Alec Davies
  • 127
  • 11