1

I'm trying to understand how the file systems work in PHP. I created a small example to display an image from a folder but instead of printing the image I think I'm printing the contents.

<?php
  $image = "/path/to/image.jpg";
  $myfile = fopen($image, "r") or die("Unable to open file!");

  $logo = fread($myfile,filesize($image));
  echo"<img src=\"$logo\" width=\"100\" height=\"100\"\/>"; 
?>

Did I happen to write a command wrong or miss something I needed to do?

2
  • I don't fully understand your objective. do you try $image = 'imagepath'; echo '"<img src=\"$image\" width=\"100\" height=\"100\"\/>" ?; Commented Apr 17, 2015 at 17:23
  • 1
    Thanks for all the responses! I tried the exercise above with echo "<img src='$log'> before but I just wanted to see how it was done with the file contents and encodeing in base 64 worked! Commented Apr 17, 2015 at 17:40

3 Answers 3

1

Your data is not a path,but the file's content, and it is OK, but you must use data URI syntax:

$logo = "data:image/jpg;base64,".base64_encode($logo);
echo"<img src=\"$logo\" width=\"100\" height=\"100\"\/>"; 

See similar thread here: Unable to display image from MySQL table

Sign up to request clarification or add additional context in comments.

Comments

0

The img src attribute takes a filename and does it's own process to read the image and actually display it. If you open an image file with a text editor you'll get interesting gibberish. What you tried to do in the above code is put that gibberish in the src. You can see that if you do View Source in your browser.

If you want to show an image you don't actually need php, this would simply be <img src="/path/to/image.jpg"> providing the path is inside your webroot.

Comments

0

You need to provide the image url as the src and NOT it's content, like this :

$log = "http://ideone.com/gfx2/img/spoj.png";

echo "<img src='$log' width='100' height='100'/>"; 

DEMO

Let's say the image is on the same directory as the html page, then you can simply use:

$log = "spoj.png";
echo "<img src='$log' width='100' height='100'/>"; 

Let's imagine the image is inside a directory named images on the same directory as the html page, then you can use:

$log = "images/spoj.png";
echo "<img src='$log' width='100' height='100'/>"; 

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.