简体   繁体   English

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

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

I have a project that requires a site visitor to fill form part of which is file upload. 我有一个项目,需要网站访问者填写表单,其中一部分是文件上传。 I intend the client to be able to view the picture once the button is clicked; 我希望客户端能够在单击按钮后查看图片; the data is not stored in any folder or database for now; 该数据目前尚未存储在任何文件夹或数据库中; I just want a page to load the form data. 我只希望页面加载表单数据。 My code is not working: when I click the button a dialog box pops up asking me what I want to do with the php file. 我的代码不起作用:当我单击按钮时,会弹出一个对话框,询问我要如何处理php文件。 Options iclude: open with firefox, save etc. Pls, where did I err? 选项包括:使用Firefox打开,保存等。请问,我在哪里出错? Here's the processphoto_form.php 这是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 的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>

If you're sure the uploaded image is a jpeg and it's square, you could do the following: 如果您确定上传的图片是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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM