|root
| subdir 1
| subdir 2
| subdir 3
| subdir 4
| subdir 5
| myphp.php
|relativeroot2(images directory)
img1.jpg
In the above myphp.php script Iam trying to include the (img1.jpg)from relativeroot2 dir.Iam not able to do that how can i include it . when i say $_SERVER['DOCUMENT_ROOT']
iam returned with value as "root" from ther how can include it <img src ="<?php echo $_SERVER['DOCUMENT_ROOT']?>"/relativeroot2/img1.jpg">
Assuming this is how your web folder looks
root
/includes
/relativeroot1
/relativeroot2
Create a file named path.php
in the includes
folder above and insert the following
<?php
if (session_id() == '') {
session_start(); /* if not already done */
}
/* replace value below with appropriate header to root distance of this include file */
$header_to_root_distance = 1;
$header_dir = dirname(__FILE__);
$root_distance = substr_count($header_dir, DIRECTORY_SEPARATOR) - $header_to_root_distance;
if($_SERVER['SERVER_ADDR']){
$include_distance = substr_count(dirname($_SERVER['SCRIPT_FILENAME']), "/");
}else{
$include_distance = substr_count(dirname($_SERVER['SCRIPT_FILENAME']), "\\");
}
$r_path = str_repeat('../', $include_distance - $root_distance);
$_SESSION['r_path'] = $r_path;
/* Note - $r_path holds your relative url starting from the root folder */
?>
Create a .htaccess
file in the root
folder above and insert the following
<IfModule php5_module>
php_value include_path "includes/"
</IfModule>
Now open the file you want a relative path e.g your myphp.php
script and insert the following at the top
<?php require 'path.php' ?>
So to request for any relative path from any file in your case. Just do the following
<img src="<?php echo $r_path.'relativeroot2/img.jpg' ?>" title="My Image" />
If your diagram is showing that relativeroot2 is outside of root, but in the same parent directory, you can just use ../
to go up one level before specifying it. You also have a stray double-quote in your image tag.
<img src ="<?php echo $_SERVER['DOCUMENT_ROOT']?>/../relativeroot2/img1.jpg">