简体   繁体   中英

use header.php file in different directories

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 (eg 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)

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.

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 AJ '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.

像上面一样使用,因为文件路径在您的情况下应该是正确的,而不是访问图像文件的正确方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM