Nibur
Nibur

Reputation: 49

How to display an image from a folder using PHP variable in a HTML tag?

I just want to display an image from a directory using PHP. But I could not. I have tried all the below ways.

enter image description here

<?php

/* Getting file name */
$document_root = $_SERVER['DOCUMENT_ROOT']; 
$replace =  str_replace('/',"\\",$document_root);
$filename = "/excel.png";
//$image_path = $replace."\imagesupload\uploads".$filename;
$image_path = $document_root."/imagesupload/uploads".$filename;
echo $image_path;  
 //echo '<img src="../admin/upload/' . $display_img . '" width="' . $width . 'px" height="120px"/>';
?>
<html>
    <body>
        <img src="<?php echo $image_path; ?>" />
   </body>
</html>

The above code gives the following output. But the path I gave was correct.

Upvotes: 1

Views: 1720

Answers (4)

Rubin Anbin
Rubin Anbin

Reputation: 328

I think this one will help you

<img src="uploads/<?php echo $file.png;?>" />

Upvotes: 1

LordNeo
LordNeo

Reputation: 1182

The DOCUMENT ROOT variable gives the absolute path of the folder in the server machine

DOCUMENT_ROOT=/var/www/example
DOCUMENT_ROOT=C:/wamp/httpdocs

What you're looking for is the relative path on the domain, so you should be using HTTP HOST

HTTP_HOST=www.example.com

That way you can build up your image url using the website absolute path and not your computer's absolute path.

Of course you can always just build a relative path to the folder you're in:

Assuming the script is in the www folder:

<img src='./<?=$filename;?>' />

Upvotes: 0

TsV
TsV

Reputation: 639

this should work:

    $filename = "/excel.png";
    $image_path = "/imagesupload/uploads".$filename;
    echo '<img src="' . $image_path. ' "/>';

Upvotes: 0

Jatin Parmar
Jatin Parmar

Reputation: 2910

you are providing a path to file which belongs to server-side and not accessible by the browser , if imageuploads is the root of your application you can do something like this:

<?php
   $filename = "excel.png";
?>
<html>
  <body>
       <img src="/uploads/<?php echo $filename?>" />
 </body>
</html>

Upvotes: 1

Related Questions