1

I have a webpage with this structure:

/var/www/html/webpage/
                    |-stands/
                    .      |-header.php
                    .
                    |-images/
                    .      |-picture.png
                    .
                    |-login/
                    .      |-login.php
                    .
                    |-index.php

I'd like to use the stands/header.php in both files, the login/login.php and the index.php. I included it in both files with

<?php
include("stands/header.php")
?>

or

<?php
include("../stands/header.php")
?>

This works fine until header.php uses a graphic from images/ folder like images/picture.png.

I use the picture as follows in the header.php:

<img src="../images/picture.png" alt="nice pic"/>

I get the picture.png in the login.php file but in the index.php i get nice pic.

It seems like, if I include the header.php file the directory where it looks for the picture starts not at the directory of the header.php but from the directory where it is included (e.g. from index.php, it looks in the 2nd parent directory of index.php which is html/ (because of the ../ and there is no images/ folder, so I get nice pic instead of the .png file.

My question now is:

Is there a way to include graphics global, means that it always (always) looks for the same directory?

Thanks for any help and sorry for my bad structured question (I'm not English and don't know how to ask this more simple)

4
  • does your website's URL point to the folder webpage? Commented Jul 20, 2019 at 12:19
  • @AJ not yet, but I'll configure it Commented Jul 20, 2019 at 12:48
  • Then you can try my answer and check if it works for you. Commented Jul 20, 2019 at 12:48
  • Just wanted an update. Did this solution work for you? Commented Jul 22, 2019 at 6:18

3 Answers 3

1

The image from your img tag is loaded on the client side, so it is not related to php. Your image tag src must be relative to the visitor current location.

You can use absolute path like A J's answer, that would be a solution, but in your case it seems to me like your login.php file should be in the same directory than your index.php. Unless you really need it that way, i think you should try to put php scripts that will be loaded directly, at root level of your site, and inluded scripts and images in folders.

Sign up to request clarification or add additional context in comments.

Comments

1

Try giving a absolute path to your image.

<img src="<?php echo 'http://'.$_SERVER['HTTP_HOST']; ?>/images/picture.png" alt="nice pic"/>

It will take the current URL and merge it with the image path.

Comments

0

use like above because file path should be correct in your case is not the correct way to access the image file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.