简体   繁体   中英

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. Options iclude: open with firefox, save etc. Pls, where did I err? Here's the 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>

If you're sure the uploaded image is a jpeg and it's square, you could do the following:

    $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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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