user1592947
user1592947

Reputation: 23

Using absolute path for images in localhost

I want to use absolute path for all of my files in the server. I want to include files that exit in "include" folder but those files contain images that they have src and when I include them in my other files in other directories the server gives an error because the path to the image is not right.

The only solution I found for this problem was to create constant file and add absolute addresses like this: localhost/include/myfile.php it works for all the includes but not for images. Instead browser changes the src of those images to C:/www/localhost/include/myfile.php or something like that which destroys the address.

Is there any way that I can add absolute path to my include file , images, css files , js files and let me mention that : http://www.mydomain.com/myfiles does not work because I'm working in localhost

Upvotes: 2

Views: 3755

Answers (1)

EasyCo
EasyCo

Reputation: 2036

Use the following PHP global var to build your path

$_SERVER['DOCUMENT_ROOT']

To get your base path. then you just append it to the filename/directory you want.

For example

echo $_SERVER['DOCUMENT_ROOT'];
=> /User/trev/Sites/testsite/public_html/
include $_SERVER['DOCUMENT_ROOT'].'/include/func.php';

Upvotes: 1

Related Questions