images not showing with require_once

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vinpkl
    New Member
    • Oct 2008
    • 41

    images not showing with require_once

    hi all

    i m having problem with including files and images geting disappear.

    i have images folder on the root.
    i have hdr.php in my included_files folder on root.

    if i include hder.php in my files that are on the root with this code
    Code:
    <?php require_once("included_files/hdr.php");?>
    then all the images of the header.php are shown perfectly alright.


    But

    if i include header.php in my files that are inside any folder on the root with this code
    Code:
    <?php require_once("../included_files/hdr.php");?>
    then all the images of the header.php are not shown. they disappear.


    Like i have folder2 on root. and there is file2 inside folder2. Then i used this code
    Code:
    <?php require_once("../included_files/hdr.php");?>
    to include my header file. but with this code my images of header.php file disappear.


    i m not able to understand as we use "../" if the file is inside in any folder on root.

    when i view the html source in explorer then i get the same path on both pages whether the page is inside any folder or on root.
    Code:
    <img src="images/home_icon.gif" alt="" width="13" height="12" style="padding-bottom:5px" />
    thanks
    vineet
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    image paths do not change with the path of the PHP file you're in.

    If you include a header in a php file that's inside a folder then your images in header need to be changed and have ../ added to them.

    Header.php file get's pulled into your subdirectories file as if you the code was exactly put there. And of course the img folder is not relative to this subdirectory and therefore it will now show.

    If you have the following

    index.php
    img/
    img/foo.jpg
    include/
    include/header.php
    folder1/
    folder1/test.php

    and you include header.php in test.php the images in header must be relative to test.php (such as src="../img/foo.jpg" )

    if you include header in index.php, your images must be relative to index.php.
    (such as src="img/foo.jpg" )


    HOW TO SOLVE THE PROBLEM:

    either correct your relative paths. (copy images to subdirectories) or use the FULL address for the image (ie http://yourdomain.com/img/foo.jpg )

    Good luck,





    Dan



    Originally posted by vinpkl
    hi all

    i m having problem with including files and images geting disappear.

    i have images folder on the root.
    i have hdr.php in my included_files folder on root.

    if i include hder.php in my files that are on the root with this code
    Code:
    <?php require_once("included_files/hdr.php");?>
    then all the images of the header.php are shown perfectly alright.


    But

    if i include header.php in my files that are inside any folder on the root with this code
    Code:
    <?php require_once("../included_files/hdr.php");?>
    then all the images of the header.php are not shown. they disappear.


    Like i have folder2 on root. and there is file2 inside folder2. Then i used this code
    Code:
    <?php require_once("../included_files/hdr.php");?>
    to include my header file. but with this code my images of header.php file disappear.


    i m not able to understand as we use "../" if the file is inside in any folder on root.

    when i view the html source in explorer then i get the same path on both pages whether the page is inside any folder or on root.
    Code:
    <img src="images/home_icon.gif" alt="" width="13" height="12" style="padding-bottom:5px" />
    thanks
    vineet

    Comment

    • vinpkl
      New Member
      • Oct 2008
      • 41

      #3
      images not showing

      hi dlite

      making my images path change to
      Code:
      <img src="http://localhost/test/images/samsung_logo.gif" width="111" height="50" />
      works fine and show images on all pages whether page is in any folder or outside on the root.

      but with this change first for localtesting i have change all images path to
      Code:
      <img src="http://localhost/test/images/samsung_logo.gif" width="111" height="50" />
      then again for live server view i have to change the all images path in all pages to
      Code:
      <img src="http://domain.name/images/samsung_logo.gif" width="111" height="50" />
      is there anything we can put in some variable for images and images can take path from there.

      vineet

      Comment

      • dlite922
        Recognized Expert Top Contributor
        • Dec 2007
        • 1586

        #4
        yes, define a constant in a config file somewhere that should contain your site-wide variables (Database login, etc)

        Code:
        define("BASE_URL","http://domain.com/");
        in the images you would do

        Code:
        <?php echo "<img src='" . BASE_URL . "' alt='My Image' />"; ?>




        Originally posted by vinpkl
        hi dlite

        making my images path change to
        Code:
        <img src="http://localhost/test/images/samsung_logo.gif" width="111" height="50" />
        works fine and show images on all pages whether page is in any folder or outside on the root.

        but with this change first for localtesting i have change all images path to
        Code:
        <img src="http://localhost/test/images/samsung_logo.gif" width="111" height="50" />
        then again for live server view i have to change the all images path in all pages to
        Code:
        <img src="http://domain.name/images/samsung_logo.gif" width="111" height="50" />
        is there anything we can put in some variable for images and images can take path from there.

        vineet


        Dan

        Comment

        • vinpkl
          New Member
          • Oct 2008
          • 41

          #5
          images not showing

          hi dlite

          thanks very much. your code worked great.

          now everything is working fine and showing up as i wanted.

          vineet

          Comment

          • dlite922
            Recognized Expert Top Contributor
            • Dec 2007
            • 1586

            #6
            Glad I helped!

            See you around,




            Dan

            Comment

            Working...