Find the ROOT path ...

  • Thread starter Thread starter Anonymous
  • Start date Start date
A

Anonymous

Guest
Hi alll

do we have a function or something to find the ROOT path ?
My problem is I have some images and file in
two directories [images] and [includes] ...
when I want to call them from the root I can
call them like this :

includes/BlaBla.php
or
images/BlaBla.jpg

but whenever I want to call them from a sub directory
I have to call them like this :

../includes/BlaBla.php
../images/BlaBla.jpg

is there any way to call the ROOT directory and not to use ../
somethig like this :

ROOT/includes/BlaBla.php
ROOT/images/BlaBla.jpg

so I dont have to change my code everytime I create a new sub folder.

Please help me on this
Best regards
Woozy
 
Well
Ive found it :D
http://www.php-forum.com/p/viewtopic.php?t=4206&highlight=root
Cheers!
 
:eek:
nope , it doesnt work good !

Warning: main(/home/sites/mysite.com/public_html//includes/myheader.php): failed to open stream: No such file or directory in /home/sites/mysite.com/public_html/preview/index.php on line 5
 
WoozyDuck said:
is there any way to call the ROOT directory and not to use ../
somethig like this :

ROOT/includes/BlaBla.php
ROOT/images/BlaBla.jpg

/images/BlaBla.jpg

/ means root.
 
Thank you for the answer ,
bu Ive tried that one before and I got the same error :

Warning: main(/includes/myheader.php): failed to open stream: No such file or directory in /home/sites/mysite.com/public_html/index.php on line 3

Warning: main(): Failed opening '/includes/myheader.php' for inclusion (include_path='.:/usr/share/pear') in /home/sites/mysite.com/public_html/index.php on line 3

there IS a directory name [includes] and there IS a file named [myheader.php] in that directory , but what is the problem ?
:sad:
 
try use next:
$ROOT = $_SERVER['DOCUMENT_ROOT'];
include ($ROOT . "/images/bo.jpg");
 
WiZARD said:
try use next:
$ROOT = $_SERVER['DOCUMENT_ROOT'];
include ($ROOT . "/images/bo.jpg");

Well thats what Ive found before as I mentioned :

WoozyDuck said:
Well
Ive found it :D
http://www.php-forum.com/p/viewtopic.php?t=4206&highlight=root
Cheers!

but it has a problem , whenever I use this to show an image
and to get its path actually , the path will be like this :

http://www.mysite.com/home/sites/mysite.com/public_html/images/blabla.jpg


I uses like this :

$ROOT = $_SERVER['DOCUMENT_ROOT'];
$imagePath = $ROOT.'images/blabla.jpg';

but the path will be like the one I show before !!!

how can I fix this ?
 
Your problem is that you have two completely separate problems here. In one of them, you're referring to an image. When you show an image on a web page, you refer to it by everything after the document root, e.g. public_html. So if the image is located in /home/public_html/images/image.png, your IMG tag will look like this:
Code:
<img src="images/image.png"/>
When you're including a file via PHP, you're referring to something to its location on the actual filesystem of the server, NOT the directory structure of the web site. The DOCUMENT_ROOT variable refers to the /home/public_html part (i.e. the root of your web site). So if you want to include a file that's located in /home/public_html/script/script.php, you can refer to it like:
Code:
include $_SERVER['DOCUMENT_ROOT'] . '/script/script.php';
 
Well it still doesnt work! :(
I will draw the situation here :

I have a tree like this :



[SITE ROOT]-
..... index.php
------------- [includes]
...................... contents.php
------------- [images]
...................... blabla.jpg
------------- [test]
...................... index.php



Ok , My first [index.php] which is in ROOT DIRECTORY includes some data from the [contents.php] file from [includes] directory. It works fine , I even can show image [blabla.jpg] from [images] directory perfectly.

now , when I want to do the same from [index.php] which is in [test] directory , I have to include that file [contents.php] again! so I do it like this :
include '../includes/contents.php';

it works again , but the functions in [contents.php] cannot read the images , because they use [../images/blabla.jpg] as the path , so whenever I call the second index file , the image path should be changed to [../../images/blabla.jpg] !!! but as you can see I cannot change those path everytime I call my functions from [contents.php] file , maybe I want to have hundreds of directories , so I have to make this change everytime which is horrible !

My goal is finding a command which is like a root or something , so whenever I call the functions in [contents.php] , (it doesnt matter where from) , they can read the images perfectly , because if we call a function from a sub directory , PHP will use that directory as the current one , but if we have a command like the one you told me , I can go to the root , and then read the images and other files from there , so I can include my [contents.php] whereever I like and it is not depend on the subdirectory.

Ive tried and used the the command you told me like this :

include $_SERVER['DOCUMENT_ROOT'] . 'includes/contents.php';

it works , it reads the file perfectly , but the path for images will be like this :

<body background="/home/sites/mysite.com/public_html/images/blabla.jpg">

and it cannot read that file !
but if I change it to :

<body background="http://www.mysite.com/public_html/images/blabla.jpg">

then it works !!!!!!!!!!!!

could you please help me on this ?
 
Weheeeeeeeeeeeee

Ive found it ....

for Images or CSS I used backslash :

<img src="/images/blabla.jpg"/>

but for PHP files Ive used DOCUMENT_ROOT :

include $_SERVER['DOCUMENT_ROOT'] . '/includes/contents.php';

THEY BOTH WORK ,
Thank you very much swirlee ,
I was confusing because you forgot to put BACKSLASH
in the first code line for images ;)

but thanx a lot for your useful tips ,
I finally did it :D

Cheers!
 
WoozyDuck said:
I was confusing because you forgot to put BACKSLASH
in the first code line for images ;)

You're confused. This is a slash: /

This is a backslash: \
 
:-D
I know mate
I was happy ,
hahahahaha
btw it works :D
with slash or backslash
:D :D :D
Cheers!
 
Back
Top