Home > Blockchain >  php route - how I can display good image path
php route - how I can display good image path

Time:12-25

I am using PHP and trying to display an image in a view. Here is the code that displays the image.

if ($annonce->avatar)
            $res .= '<a href="../src/'.$annonce->avatar.'" alt="avatar" >Lien</a>';

In src folder I have

  • avatar folder

image.png ^

  • controller folder
  • entity folder

^ Actual file where I am and where the code is executed but the path is not correct How I can display it please ?


In template folder I have:

  1. annonce

^ index.php The file in question who call entity functions

  1. home
  2. favoris

EDIT: the image is not displayed, even with the img tag

$res .= '<img src="'.dirname(__DIR__).'\avatar\\'.$annonce->avatar.'" alt="avatar">Lien</img>';

CodePudding user response:

I will answer your question as I understood you, because I miss some vital info about your system. But I think you have some problems understanding routing in php and the browser.

Folder structure:

root/website_x/
- src/
  - avatar/
    - image.png
  - controller/
  - entity/
  - ...
- template/
  - annonce/
     - index.php
     - ...
  - home/
  - favoris/
  - ...
- index.php
- index.html
- ...

I guess you have this url: localhost/website_x/index.php

If you have a real server and a domain, your path would be like: www.website-x.com

Truth is, www.website-x.com points at root/website_x/index.php.

  1. the browser handles a path different than the server (php)
  2. the template/annonce/index.php is not used directly. It is included via magic of the website.

When it come to using files in the browser, you have to think like the browser. If you browser opens the url www.website-x.com, than the root is where the server tells him where it is. In our example, it is www.website-x.com/(index.php). The browser does not know the folderstructure of the server.

So the fact is, if you place an <img src="../src/avatar/image.png"> the browser cant find it because it looks there www.website-x.com/../src/avatar/image.png what is mostly wrong. Thats why I told you to open the image in the browser via url. But www.website-x.com/src/avatar/image.png should display your image in the browser. Or localhost/website_x/src/avatar/image.png if you use xampp.

What you can do with php to tell the browser the correct path:

Your $annonce->avatar variable should contain the path beginning from the root of the website. Means localhost/website_x/src/avatar/image.png or www.website-x.com/src/avatar/image.png. The path src/avatar/image.png or even /src/avatar/image.png can (and possibly will) cause other trouble.

Other things which could prevent your browser from displaying your image, even with a correct path:
  • htaccess forbids that
  • webserver does not have the rights to access the image
  • image file is corrupt

CodePudding user response:

To display an image, you should not use the <a href="SRC_IMG"></a> tag but the <img src="IMG_SRC"> tag...

CodePudding user response:

you can put img tag in under anchor <a href tag

  •  Tags:  
  • php
  • Related