Can i use $_SERVER['DOCUMENT_ROOT'] to put the path of a img?
Why this wont work?

$imgLocation = $_SERVER['DOCUMENT_ROOT'].'/images/logo.jpg';
echo '<img src="'.$imgLocation.'" alt="logo.jpg"';

Recommended Answers

I believe you are looking for:
$_SERVER['SERVER_NAME']

DOCUMENT_ROOT will give you a local path on the host machine, which will begin with a drive letter and not a protocol (like http/s).

DOCUMENT_ROOT is more appropriate when doing includes where you will be traversing a file system, not when making calls …

Jump to Post

That would only work if your loading the page locally (If you double click the HTML file to open it in you default browser). If you try that through a web address on a server it will not work, the browser will not allow a page access to the C …

Jump to Post

All 7 Replies

I believe you are looking for:
$_SERVER['SERVER_NAME']

DOCUMENT_ROOT will give you a local path on the host machine, which will begin with a drive letter and not a protocol (like http/s).

DOCUMENT_ROOT is more appropriate when doing includes where you will be traversing a file system, not when making calls over a network.

in reality, depending on how you have configured your server, you probably don't need $_SERVER['SERVER_NAME'] and you can probably use a relative path instead.

So i tried this And it wont work either

Html

<img src="C:/xampp/htdocs/images/logo.jpg">

Im guessing This can not be done? Although i remember i did it in the past...

I believe you are looking for:
$_SERVER['SERVER_NAME']

I tried this too Wont work either, It echoed this
localhost/images/logo.jpg

Thats the correct path.. but i think since im on local host it and not from a http... Its searching for the folder "localhost"

Like this

<img src="localhost/images/logo.jpg">

That would only work if your loading the page locally (If you double click the HTML file to open it in you default browser). If you try that through a web address on a server it will not work, the browser will not allow a page access to the C drive, not to mention that it would be looking on the c drive of whoever is looking at the page, not of the server.

You can do the following, but again it will only work on your machine:

<img src="file://C:/xampp/htdocs/images/logo.jpg">

OR

<img src="http://localhost/images/logo.jpg">

Hope that helps

So i was right: if i put http:// before
will display the img...

$imgLocation = 'http://'.$_SERVER['SERVER_NAME'];

But if i put this.... when i decide to update my page will echo 'http://http://webpage.com/images/logo.jpg'
Right?

I do not believe SERVER_NAME includes the protocol (so, no, it should not show http://http://)

I do not believe SERVER_NAME includes the protocol (so, no, it should not show http://http://)

In that case, thank u sir!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.