3

How do i display the image uploaded after the form has been submitted? After the form is submitted it will be a preview page, so i'm not storing the image type BLOB in MySQLyet. How do i display $_FILES['image']?

<?php

if(isset($_POST['submit'])) {

 //preview page

$info = getimagesize($_FILES['image']['tmp_name']);
$imagetype = $info['mime'];
$tmpname = $_FILES['image']['tmp_name'];
$fp = fopen($tmpname,'r');
$data = fread($fp,filesize($tmpname));
$data = addslashes($data);
fclose($fp);

} else {
?>

<form enctype="multipart/form-data" action="http://www.example.com/submit" method="POST">
<input type="file" name="image" value="" size="50" />
<input type="submit" name="submit" value="submit" />
</form>

<?php
}

?>
user892134
  • 3,078
  • 16
  • 62
  • 128
  • 1
    Blobs in a database? [Why do you hate kittens?](http://stackoverflow.com/questions/3401761/remote-image-file-to-sqlite-blob-in-php/3401861#3401861) – NullUserException Nov 12 '11 at 08:52

3 Answers3

13

If you don't want to store the image on the server you can preview like this:

<?php
if (isset($_FILES['image'])) {
    $aExtraInfo = getimagesize($_FILES['image']['tmp_name']);
    $sImage = "data:" . $aExtraInfo["mime"] . ";base64," . base64_encode(file_get_contents($_FILES['image']['tmp_name']));
    echo '<p>The image has been uploaded successfully</p><p>Preview:</p><img src="' . $sImage . '" alt="Your Image" />;
}

But if you want to store it on the server, just store it and show it without changing the page.

noob
  • 8,982
  • 4
  • 37
  • 65
  • This sounds like a bad idea. I heard that you can do stuff like this in JavaScript though easily. – noob Jun 17 '16 at 04:28
0

i'm not storing the image type BLOB in MySQLyet. How do i display $_FILES['image']?

exactly the same way as if you stored it.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
0

As "tmp_name" could suggest it's a temporary resource. Save it to disk, then show it, then remove it

 <?
 $preview_file = false;
 if(is_uploaded_file($tmpname) && $info=@getimagesize($tmpname)){
   $preview_file = "./tmp/tmp_preview.extension";
   move_uploaded_file( $tmpname, realpath($preview_file) );
 }
 if($preview_file){
   // show preview
 }
lcapra
  • 1,420
  • 16
  • 18