Images in header not display when included in files of different folders even the path is correct

Viewed 128

I'm new to programming & this is a simple project. Mainly I have assets,model,view folders.my problem is I have a header.php file with images which is common to all files.so my images are in img folder which is inside assets folder.So my path to images is

< img src="assets/img/logo.png ">

When I include the header in index.php file it works properly.

require_once('header.php');

But when I include header into the user.php file which is inside userManagement folder inside view folder even the header is added images are not displaying. Code is require_once('../../header.php');

folder structure is

assets

img

 logo.png

model

view

userManagement

       user.php

index.php

header.php

Please help me with this.Thanks in advance.

1 Answers

This happens because index.php and user.php are not in the same directory level. So, when including header.php inside index.php, the path to the images is assets/img/, which is correct. However, when you include header.php inside user.php, the path to the images is also assets/img which is incorrect. If I correctly understand what you've written, then your folder structure looks like this:

+ assets
  + img
    - logo.png
+ modal
+ view
  + userManagement
    - user.php
- index.php

Hence, you should refer to your images in this way:

../../assets/img/

You can go around this by adding this line to your user.php in the <head> section:

<base href="../../"/>

However, all URLs in user.php should be made to point to their targets as if they were two levels down in the main directory.

Related