Mark Kennedy
Mark Kennedy

Reputation: 1781

PHP - display POSTed image

I'm sending a .jpeg to the below server using an HTML form

How do I get PHP to successfully display the image once the server receives it?

<?php 
       if ($_FILES["file"]["error"] > 0)
          {
              echo "Error: " . $_FILES["file"]["error"] . "<br>";
          }
        else
          {
              echo "Upload: " . $_FILES["file"]["name"] . "<br>";
              echo "Type: " . $_FILES["file"]["type"] . "<br>";
              echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
              echo "Stored in: " . $_FILES["file"]["tmp_name"] . "<br>";
          }

        $img = $_FILES["file"];

        echo $img;

?>

Upvotes: 0

Views: 2305

Answers (2)

mavrosxristoforos
mavrosxristoforos

Reputation: 3643

You should

 move_uploaded_file($_FILES["file"]["tmp_name"],
                    $target_path.'/'.$_FILES["file"]["name"])

So that you move the temporary file and then you can show it with

 print '<img src="http://yoursite.com/url_of_target_path/'.
          $_FILES["file"]["name"].'"/>

Beware not to use the exact code I provided, before sanitizing uploaded data.

Upvotes: 0

Mithun Satheesh
Mithun Satheesh

Reputation: 27835

you have to set the content header to the corresponding image format before you echo $img

for example if the uploaded image is jpg it should be

header('Content-Type: image/jpeg');
echo $img;

also make sure that you are not echoing text data along with it.

Upvotes: 1

Related Questions