0

i'm trying to display an image using php to include its full path but the picture doesn't want to show up, the code i'm using is :

<img src="<?php echo $_SERVER['DOCUMENT_ROOT'] . 'Romeo/Yoomak/pix/Logo9.png';?>" width="200" height="200" alt="Logo"/>

No Error shows up in the output but the picture doesn't show up so what should i do ? help pls and thnx in advance.

That's what i get: image not showing up as shown in this SS

Ahmus
  • 3
  • 4
  • What do you see when you take a look at the generated path within `src`? – B001ᛦ Jul 23 '21 at 19:07
  • @B001ᛦ I don't see anything abnormal, everything seems ok and the page loads normally but the picture is not showing up on it – Ahmus Jul 23 '21 at 19:17
  • _everything seems ok..._ What does that mean? Can you copy and paste the path into the browser and see if the picture loads directly? – B001ᛦ Jul 23 '21 at 19:23
  • What do you see when you `echo` the generated path with `src`? – renshul Jul 23 '21 at 19:25
  • @renshul I just added a link with a SS showing what i get please check it – Ahmus Jul 23 '21 at 19:46
  • @B001ᛦ I just added a link with a SS showing what i get please check it – Ahmus Jul 23 '21 at 19:47
  • Again, can you copy and paste the generated path into the browser and see if the picture loads directly? – B001ᛦ Jul 23 '21 at 19:59
  • @B001ᛦ you mean just write the full path of the image in the browser and see if it shows up ? – Ahmus Jul 23 '21 at 20:03
  • Is your HTML browser viewing your web page as an online page or a "local" machine page? – Martin Jul 23 '21 at 20:27
  • Please read [**This Q&A**](https://stackoverflow.com/questions/11927968/document-root-php) – Martin Jul 23 '21 at 20:29
  • @Martin I'm using XAMPP local server atm and the picture shows up just fine if i wrote the path of the image without $_SERVER['DOCUMENT_ROOT'] part as a pure html image tag with the full path of the image but when i try to use the php in the image tag, the picture disappears – Ahmus Jul 23 '21 at 20:33
  • @Ahmus `$_SERVER['DOCUMENT_ROOT']` should never be used as a reference address in HTML. Simply use HTML root reference `/` – Martin Jul 23 '21 at 21:08
  • @Martin the problem is when i use plain html root ref, when i try to include the page that has the picture in it into another page in a different directory, the image doesn't show up so there got to be some other dynamic way to do it other than the html way – Ahmus Jul 23 '21 at 21:29

2 Answers2

1

You need to add "/" before Romeo. Or you set an incorrect path here: 'Romeo/Yoomak/pix/Logo9.png'

Donia Mark
  • 11
  • 2
1

The Document Root is the directory on filesystem of the computer running the HTTP server that corresponds to the / path in the server's URL.

The browser will ask the HTTP server for an HTTP URL.

The browser can't read files directory from the server's filesystem (it would be a dreadful security problem if it could).

Assuming that the file really is in that path from the document root, you should have simply:

<img src="/Romeo/Yoomak/pix/Logo9.png" width="200" height="200" alt="Logo"/>

NB: Logo is pretty dreadful alt text

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I thought this question was a dupe but I can't find an exact dupe of it (probably ambiguous wording) – Martin Jul 23 '21 at 21:08
  • @Quentin It works fine when i do it plain html like u did but the thing is when i try to include this same page that has the image into other pages which are in different directory the image disappears because this plain html way is not dynamic, that's why i tried to use php in the img src attribute in order to be able to show the image wherever i include its page in – Ahmus Jul 23 '21 at 21:12
  • 1
    @Ahmus — Paths starting with a `/` are absolute. It shouldn't matter where the HTML document is so long as it is on the same server (and hostname/port). Maybe you missed the `/` at the start of the URL in this answer. – Quentin Jul 23 '21 at 21:16