Hi

I have a puzzling problem with my PHP includes.

I am using IIS and I configured it to run php, but maybe I forgot to do something because my include functions only work in very specific cases and I need help to understand why and how I can fix it.

I have NO TROUBLE with includes from within the SAME directory when all files are in the same directory, including images.

The TRUE Problem:

I want to include a file from another directory. This included file has images in it. These images come from yet another directory. BUT I can only get the images to appear if I use an absolute path beginning at the site root folder. why?

In my original file, my code looks like this:

<?php
include 'includes/inside_header.html';
?>

In included file (inside_header.html), the code to get my image looks like this:

<IMG SRC="/PHP_SQL_testing/IJC_CMI/rel/2003biennial/images/footer_3.gif" WIDTH="8" HEIGHT="91" />

This code WORKS.

===> But what do I do if I want to use a RELATIVE path to go get my image???

I have been trying all day and I would love some help!!

I am building on windows 2000, but the site will reside on a unix server.

Thanks!!

    What's a relative path again? Why do I forget these things?

    Anyhoo, I'm guessing this is on windows. Why not just make the image code like this: <IMG SRC="C:\PATH\TO\IMAGE" WIDTH="8" HEIGHT="91" />

    Ignore this post if this isn't a relative path...

      I have the same configuration...

      I use:

      include("$DOCUMENT_ROOT/folder/file.inc");

      in this case you have to use the same root-structure like on remote. Because I have multiple sites on my localhost I have created some virtual hosts.

        If it's relative to the page then:
        <?php require_once('../folder/file.php'); ?>

        works

        If its Unix and you wan't to do link relative to root
        <?php require_once($_SERVER['DOCUMENT_ROOT'] . '/folder/file.php'); ?>

        I've tried Olaf's solution and can't get it to work and my site is on IIS. If anyone else knows I'll be interested

          What did you get on IIS if you execute?

          echo $_SERVER['DOCUMENT_ROOT'];

            Sorry Olaf
            It does work if you set the variable $DOCUMENT_ROOT to your site address first. So do that and only once for each page

            <?PHP $DOCUMENT_ROOT = "http://www.yoursite.com" ?>

            <?PHP include("$DOCUMENT_ROOT /folder/file.php"); ?>

            works

              Olaf I just tried:

              <?PHP echo $_SERVER['DOCUMENT_ROOT']; ?>

              and it came up with errors ie the environmental variable doesn't exist for Windows PHP ( I checked my phpinfo() page)

              It seems the equivalent for the windows version of PHP is

              <?PHP echo $_SERVER["HTTP_HOST"]; ?>

              which worked and returned my domain

              Andy

                Hi guys

                You gave me a few leads, so I will try them and get back to the thread. I'm still very shaky in PHP so it may take a while to test.

                Meanwhile, one thing I did find out was that the INCLUDE_PATH in the php.ini file was not set, so i'm testing that as well.

                back in a few hours with my results

                  Well thanks for all your help guys. I will be keeping all the code you posted for my further schooling. 🙂

                  I am grateful for the help, but you didn't actually answer my question. I needed to know how to make my images appear in the included file without calling the image with an absolute link. The images never appeared when I called the include with a php include function, but they did appear when I called the included page directly through the browser.

                  My solution was the following:

                  The images in the included file are now called as follows:

                  <IMG SRC="./images/curveheader_fr_2.gif" ...

                  instead of:

                  <IMG SRC="/PHP_SQL_testing/IJC_CMI/rel/2003biennial/images/curveheader_fr_2.gif" ...

                  This problem only crops up when I use a php include. other types of included files work fine when I use relative path code like this to call my images:

                  <IMG SRC="../images/curveheader_fr_2.gif" ...

                  Well, problem solved anyway... thanks for everything!

                  Ian

                    Your problem may have been because the images become relative to the parent file, not the child include file. So they need to reflect the path from the parent, not the child

                    That's why I always link relative to root and not relative to file. Then includes will always work straight away.

                      Just make sure you always experiment different methods. Also, I found that the problem is always the simplest thing you overlook because you think that can't be the problem.

                        Originally posted by andynightingale
                        Olaf I just tried:

                        <?PHP echo $_SERVER['DOCUMENT_ROOT']; ?>

                        and it came up with errors ie the environmental variable doesn't exist for Windows PHP ( I checked my phpinfo() page)

                        It seems the equivalent for the windows version of PHP is

                        <?PHP echo $_SERVER["HTTP_HOST"]; ?>

                        which worked and returned my domain

                        Andy

                        DOCUMENT_ROOT is the absolute path to the web sites home directory on the filesystem, NOT on the web. HTTP_HOST is the domain name. They are 2 different things.

                        on a linux system DOCUMENT_ROOT would be something like /home/user/www/htdocs or something like that. On a windows machine, it would include the drive letter at the start. C:\somedirectory\pathgoeshere

                        • keith

                          Hallo,

                          of course the var DOCUMENT_ROOT looks different on windows and Linux.

                          But it works on both if the folder is on the same place. I checked this:
                          My code:

                          <img src="<?php $_SERVER['DOCUMENT_ROOT']; ?>/Images/arrowVacatures.gif" width="24" height="25">

                          executed on windows it is:
                          <img src="/Images/arrowVacatures.gif" width="24" height="25">

                          and on linux its the same.
                          The only thing what i have to do is:
                          If i call the domain it must show the same page like on localhost.

                            You said--

                            Your problem may have been because the images become relative to the parent file, not the child include file. So they need to reflect the path from the parent, not the child

                            That's why I always link relative to root and not relative to file. Then includes will always work straight away.

                            =====

                            Oh geez!! :rolleyes: that's it!! so simple it completely escaped me!! I feel dumb!. But I'm glad I asked the question, cause you guys gave me great ways to ensure that this does not happen again!

                            Thanks!

                            😃

                              Write a Reply...