1

I have a Header.php file in the /image directory. I used to write the header content which contains text and images. This Header.php file is included in all other pages of different directories and sub-directories of the php project.

My problem is that when access any page from sub-directories, the image in the header.php is not showing in that page.

Thanks

fean
  • 546
  • 4
  • 12
  • 37
  • when i used the image directory src="images/image.jpg" image was showing for included in /index.php but was not showing for /display/index.php. but when i'm using src="/images/image.jpg" it's not showing for both /index.php and /display/index.php – fean Jun 18 '11 at 08:45
  • see my answer. its because when header is included in the display/index.php, it assumes that there is a directory images in display directory, and try to look up image in it, but since theres not directory, it doesnt display. – Roman Jun 18 '11 at 08:49

4 Answers4

0

Use absolute URLs. Examples:

<img src="http://yoursite.com/images/image.jpg" />

<img src="/images/image.jpg" />

This way, you aren't fighting against relative paths. Another thing you could do if you want to be lazy is use a <base> tag, which has it's pros and cons.

Community
  • 1
  • 1
Wesley Murch
  • 101,186
  • 37
  • 194
  • 228
  • not working, when i used the image directory src="images/image.jpg" image was showing for included in /index.php but was not showing for /display/index.php. but when i'm using src="/images/image.jpg" it's not showing for both /index.php and /display/index.php – fean Jun 18 '11 at 08:46
  • @fean: It *should* be working... Did you try with a full URL? If so and it works, what exactly does the URL look like? – Wesley Murch Jun 18 '11 at 08:50
  • it works perfectly for the fully specified URL like : http://xyz.com/../image.jpg but not for the second one... – fean Jun 18 '11 at 09:18
  • `xyz.com/../image.jpg` is certainly not the full URL. Can you provide the actual URL that worked? make sure to wrap it in ` backticks ` or part of it will get stripped from the comments. The `/leading/forward/slash` urls should work, I'm thinking you may be using the wrong path. – Wesley Murch Jun 18 '11 at 09:22
  • i tried in localhost, giving the full address. "http://localhost/project/images/image.jpg" – fean Jun 18 '11 at 09:27
  • Then you need an full absolute URL or you have to use `/project/images/image.jpg`. There are a myriad of other solutions especially since you can use php to output the paths dynamically. Try the `` tag even, or write a function to output your images using the correct path - that way you can just update the function if you need to change the paths. – Wesley Murch Jun 18 '11 at 09:36
0

You are probably using a relative path in your include which means that header.php may or may not be included depending on whether your current file is on the "right" directory or not.

What you can do is :

  • use an absolute path in your include
  • modify your include path depending on where the current file is located in the hierarchy
Soufiane Hassou
  • 17,257
  • 2
  • 39
  • 75
0

Probably because in the header file you have given the hard code path of the image, which is in same directory, something like "test.jpg". That's why when header file is included in sub directory, it try to look for the test.jpg image in the same directory and doesnt find it. You can fix it by using the absolute paths.

Roman
  • 3,764
  • 21
  • 52
  • 71
  • Thanks, yes, the problem with the directory, image file is not found. But with the same line of code how can it be found in a directory and as well as some other sub-directories ? – fean Jun 18 '11 at 09:36
  • @fean did you solve it already? if you havn't, probably you should write ur code in ur post, and i will try to help. – Roman Jun 18 '11 at 13:09
0

If you use require_once() instead, the site won't load unless it finds the file. You can then have an error message that tells you exactly what didn't work.

SteeveDroz
  • 6,006
  • 6
  • 33
  • 65