0

I'm making a classified website that allows users to insert an image. The image data is sent thorugh the form to the page verify.php. Is it possible to display the image on verify.php? I've tried looking online but I only found answers from people inserting the image to mysql first and then displaying it.

Here is my code for the page where I want the image to be displayed.

$name = $_FILES["image"]["name"];
$type = $_FILES["image"]['type'];
$size = $_FILES["image"]["size"];
$temp = file_get_contents($_FILES["image"]["tmp_name"]);
$error = $_FILES['image']["error"];
echo "<img src='$temp'>"; //this displays the 
                          //binary code(I beleive that's what it s called)
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Naeem Ahmed
  • 105
  • 1
  • 2
  • 9

3 Answers3

4

The value in $temp is the binary form of the image! So you can either base64_encode() it and display it this way:

echo "<img src='data:image/png;base64," . base64_encode($temp) . "'>";
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
0

You need to use move_uploaded_file method and store image somewhere in public directory of server:

$name = $_FILES["image"]["name"];
$uploads_dir = '/uploads';
$tmp_name = $_FILES["image"]["tmp_name"]
move_uploaded_file($tmp_name, "$uploads_dir/$name");
echo "<img src=\"$uploads_dir/$name\">";
Roman
  • 189
  • 1
  • 6
0

There are 3 possibilities:

Method 1: Tell your browser that veryfy.php is an image. This works with

header("Content-type: image/png");

or any other image type (depending on your result). However this method does not allow you to publish text at same time.

Method 2: Save you picture (in database or on a fileserver) and then load it with <img...>

Method 3: Use base64 encoding:

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA...">

However, this one should only be used for small files since it will slow down loading of page. More information: Embedding Base64 Images

Community
  • 1
  • 1
Klaus F.
  • 135
  • 14