显示通过网络表单上传的图像,而不存储在文件夹或数据库中


Displaying An Image uploaded via a web form without storing in a folder or database

我有一个项目需要网站访问者填写表单,其中一部分是文件上传。我希望客户端能够在点击按钮后查看图片;数据目前没有存储在任何文件夹或数据库中;我只想要一个页面来加载表单数据。我的代码不起作用:当我点击按钮时,会弹出一个对话框,询问我想对php文件做什么。选项iclude:打开firefox,保存等。请注意,我错在哪里了?这是processphoto_form.php

<?php
if(isset($_FILES['userFoto']['name']) && $_FILES['size']<=1000000){
$tempName = $_FILES['userFoto']['temp_name'];
$fhand = fopen($tempName, 'r');
$userFoto = fread($fhand, filesize($tempName));
$userFoto = addslashes($userFoto);
fclose($fhand);
$user = $_POST['user'];
header('Content-type:$userFoto/JPEG');
echo"<img scr = '$userFoto' height ='100' width = '100'/>"."<br/>";
cho$user;
}
?>

HTML

<form enctype = 'multipart/form-data' method ='post' action ='processphoto_form.php'>
<table>
<tr><td>Name</td><td><input type = 'text' name = 'user'/></td></tr>
<tr><td>Foto</td><td><input type = 'file' name = 'userFoto'/></td></tr>
<tr><td colspan ='2' ><input type = 'submit' value = 'VIEW DATA'/></td></tr>
</table>
</form>

如果您确定上传的图像是jpeg并且是正方形的,您可以执行以下操作:

    $tempName = $_FILES['userFoto']['temp_name'];    
    list($originalWidth, $originalHeight) = getimagesize($tempName);
    $src = imagecreatefromjpeg($tempName);
    $img = imagecreatetruecolor(100, 100);
    ImageCopyResampled($img, $src, 0, 0, 0, 0, 100, 100, $originalWidth, $originalHeight);
    header('Content-Type: image/jpeg');
    ob_start();
    imagejpeg($img);
    $size = ob_get_length();
    header("Content-Length: " . $size);
    ob_end_flush();
    imagedestroy($img);
    exit;