将图像存储在变量PHP中后如何打印图像[关闭]

First of all, I'm a beginner in PHP. I'm trying to design a simple application to practice handling form elements. Basically, I upload an image, push submit, and then the image gets displayed in the web browser. The form works perfectly, but I'm having trouble displaying the image once I store it into a variable.

Does anybody know how to do this?

<html>
<head>
<title>HI</title>
</head>
<body>

<form action = "blah.php" method = "POST" enctype = "multipart/form-data">
<input type = "file" name = "dope">
<input type = "submit" value = "Upload Images" name = "submit">
</form>

</body>
</html>

The PHP file is:

<?php

$image = $_FILES;
imagejpeg($image);
?>

You can simply get the mime type from the upload and read it back to the browser:

header('Content-Type: ' . $_FILES['dope']['type']);
readfile($_FILES['dope']['tmp_name']);

This will send a header with the correct mime type so the browser knows how to handle it, if it's an image it'll be handled inline, if it's an application the browser will download it. readfile reads a file and sends it to directly to the buffer.

Be aware that if you want to store the file for future reads you will want to move it and store the location in a database or similar using move_uploaded_file, e.g.

move_uploaded_file($_FILES['dope']['tmp_name'], $destination);

Then you can serve the file over and over providing $destination contains the file path:

header('Content-Type: ' . mime_content_type($destination));
readfile($destination);