0

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"';
3
Contributors
7
Replies
20
Views
3 Years
Discussion Span
Last Post by centenond
1

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.

Edited by ryantroop

0

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...

0

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">

1

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.