<?php include('../header.php'); ?> not showing images...

by 10 replies
13
Hello, I am basically "new" to PHP, but have understood it enough so far...until this...xD

Well, personally I find it a HUGE bug of which should of been fixed ages ago, but let me try explain it in as much detail as I can...

The reason I am even bothering with PHP, is that I can somewhat use it as content management system for my websites. Which works great for things like header.php and footer.php.

All means well in the root directory, until I try to call the same file in another directory.

root/index.php <--- Home Page
root/images <--- Image directory(Will not change)
root/header.php <--- Header for ALL pages
root/footer.php <-- Footer for ALL pages

So all works fine when I include the header.php and footer.php in the home page, as in those files it calls images from the root directory.

As soon as I do:

root/new-directory

and include the header.php and footer.php like so:

Code:
<?php include('../header.php'); ?>
and

Code:
<?php include('../footer.php'); ?>
The images just wont show...there has to be a way around this, and Google just keeps giving me "other" scenarios of which are not answering my questions.

Any hints? xD

Thanks in advanced.

EDIT: Typos.
#programming #&gt #&ltphp #> #<php #images #include or headerphp #showing
  • Problem solved. I added the image directory to the new directory...kind of defeats the whole purpose though...any way around this?
  • Are you using absolute or relative paths for the images in the header and footer files?
    Relative paths will look for the files in the current directory, which is why it works after you made another copy of "images" and put it in the directory.
    • [ 1 ] Thanks
  • You could 'view source' when the page is rendered in the browser and see what the path to the images is coming out as then work from there to solve the issue.

    Another way is to have a definition of where the image folder is then use that definition in all image parts:

    Put something like this in the header.php
    Code:
    define('IMAGELOCATION', 'http://www.site.com/images');
    then to display images use:
    Code:
    echo ('<img src="'.IMAGELOCATION.'/image.jpg">');
  • The best solution is to use absolute paths for any images.
  • Relative paths are preferable to absolute paths because they are more concise (contribute less bytes to overall page size) and they are portable.

    However, you don't have to worry over relative vs absolute. Just add this to your header.php file:

    Code:
    <base href="http://your-site-address.com/" />
    This is called a "Base Href" element and it should appear within the "head" section of your site. It automatically resolves any relative pathed images or media assets to the root directory that you specify in the href.

    Hope that helps!
    • [ 2 ] Thanks
    • [2] replies
    • Sure, but in this particular case, all the OP needs to do is change their to in their tags, i.e., one extra byte.
    • Wow, thank you, looks like I will need to start researching all of these useful attributes lol
  • You should check the path properly if your path is not correct in the header file for images then also it wont display
  • This may be overkill, but you could get around the maintainability issues of absolute and relative URLs by using a function to return the root directory.

    Then your image tags would look something like this:

    PHP Code:
    <img src="<?php echo root_directory(); ?>/images/logo.jpg">
    This is a direct rip-off of WordPress' bloginfo() and get_bloginfo() functions.

    Edit: Of course this also adds the complexity of an additional function definition.
    • [1] reply

Next Topics on Trending Feed