0

I have a directory tree like the below

root
    index.php
    /inc
        1levelData.php
        1level.php
        /images
            db.png

What I am trying to figure out is a way I can set an absolute path that can be used for both including files and images. In addition, a method that will work when loading both index.php as well as root\inc\1level.php I have put in some sample code below for what I am currently working with. This code seems to work well for include() but doesn't function for images. Is there a possible fix?

root/Index:

<?php
$fullPath = realpath($_SERVER['DOCUMENT_ROOT']);
echo "<img src = '".$fullPath."/inc/images/db.png'><BR>";
include $fullPath.'/inc/1levelData.php';
?>

root/inc/1levelData.php

<?php
echo "<img src = '".$fullPath."/inc/images/db.png'><BR>";
include ($fullPath.'/inc/images/2levelData.php');
?>

root/inc/1Level.php

<?php
$fullPath = realpath($_SERVER['DOCUMENT_ROOT']);
include ($fullPath.'/phpinfo.php');
?>

root/inc/2LevelData.php

<?php
echo "<img src = '".$fullPath."inc/images/db.png'><BR>";
?>
mhopkins321
  • 2,993
  • 15
  • 56
  • 83
  • 2
    Instead of messing around with `realpath($_SERVER['DOCUMENT_ROOT'])` you should set a DOC_ROOT constant which includes the fully-qualified path and include/require files that way. http://stackoverflow.com/questions/1882044/get-parent-directory-of-running-script/1882059#1882059 – Mike B Sep 25 '13 at 17:25
  • this appears to work for include statements but it doesn't mention html `` tags – mhopkins321 Sep 25 '13 at 17:45

0 Answers0