Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display $_FILES['image'] after form submit

Tags:

php

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
}

?>
like image 861
user892134 Avatar asked Dec 28 '25 16:12

user892134


1 Answers

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.

like image 189
noob Avatar answered Dec 31 '25 06:12

noob