1

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.

4 Answers 4

1

I think this one will help you

<img src="uploads/<?php echo $file.png;?>" />
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

0

this should work:

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

Comments

0

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;?>' />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.