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

by Blakos
10 replies
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.
#&gt #&ltphp #> #<php #images #include or headerphp #showing
  • Profile picture of the author Blakos
    Problem solved. I added the image directory to the new directory...kind of defeats the whole purpose though...any way around this?
    {{ DiscussionBoard.errors[8217142].message }}
  • Profile picture of the author KirkMcD
    Originally Posted by Blake Michelutti View Post

    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.

    <snip>
    The images just wont show
    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.
    {{ DiscussionBoard.errors[8217494].message }}
  • Profile picture of the author Valdor Kiebach
    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">');
    {{ DiscussionBoard.errors[8217519].message }}
  • Profile picture of the author RobinInTexas
    The best solution is to use absolute paths for any images.
    Signature

    Robin



    ...Even if you're on the right track, you'll get run over if you just set there.
    {{ DiscussionBoard.errors[8219249].message }}
  • Profile picture of the author clickbump
    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!
    Signature
    {{ DiscussionBoard.errors[8221439].message }}
    • Profile picture of the author David Beroff
      Originally Posted by clickbump View Post

      Relative paths are preferable to absolute paths because they are more concise (contribute less bytes to overall page size) and they are portable.
      Sure, but in this particular case, all the OP needs to do is change their src="images/..." to src="/images/..." in their img tags, i.e., one extra byte.
      Signature
      Put MY voice on YOUR video: AwesomeAmericanAudio.com
      {{ DiscussionBoard.errors[8223995].message }}
    • Profile picture of the author Blakos
      Originally Posted by clickbump View Post

      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!
      Wow, thank you, looks like I will need to start researching all of these useful attributes lol
      {{ DiscussionBoard.errors[8231937].message }}
  • Profile picture of the author webwizdevelop
    You should check the path properly if your path is not correct in the header file for images then also it wont display
    {{ DiscussionBoard.errors[8233904].message }}
  • Profile picture of the author dconjar
    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.
    {{ DiscussionBoard.errors[8273446].message }}
    • Originally Posted by dconjar View Post

      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.
      Nice codes, i'll use them in future Respect.
      {{ DiscussionBoard.errors[8274895].message }}

Trending Topics