Skip to content
Advertisement

Include html file with image using PHP

I am a newbie in website development.

here is my problem :

I have 2 HTML files. they are ‘index.php’ and ‘header.php’ . I try to include ‘header.php’ in to the ‘index.php’ using this code :

‘index.php’

<body>
    <?php
        include("header/header.php");
    ?>
</body>

‘header.php’ contain this code :

<h1>Its header</h1>
<img src="img/006-tumblr.png" width="200" height="200">

its the folder hirearchy :

-index.php
  --header
    --img
       -006-tumblr.png
    -header.php 

When I open ‘index.php’ , ‘header.php’ is included but the image is not displaying.

So how can I include ‘header.php’ with the image?

Advertisement

Answer

The logic of including a file into another is different from linking a CSS file. When you include a PHP file, the entire code is added to the source file and then the server compiles the codes. So the image files and other resources should be addressed relative to the source document (not the included one). This logic is different in a CSS file and the resource files e.g. a background image is complied relative to the CSS file (Because a CSS may be used in different files hirearchy). So this will work:

<h1>Its header</h1>
<img src="header/img/006-tumblr.png" width="200" height="200"

Footnote: If you want to use header in different files with different hirearchy the solution for the question above is to set BASEURL for your document and setting the resource and anchores relative to the baseurl.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement