Okay, so this works on a windows system:
index.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html dir="ltr" lang="en">
<head>
<title>Checking Uploaded Image</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<style type="text/css">
<!--
body
{
text-align: center;
}
.error
{
border: 2px groove #c10;
background-color: #f08080;
padding: 5px;
margin: 5px;
}
ul.array
{
list-style-type: disc;
border: 2px groove #006400;
background-color: #90ee90;
}
form
{
border: 2px inset #ccc;
background-color: #d7d7d7;
padding: 5px;
margin: 5px;
}
img
{
border: 2px inset #1874cd;
margin: 5px;
padding: 0;
background-color: #b9d3ee;
}
-->
</style>
</head>
<body>
<?php
$allowed_types = array(
'jpg'=>'image/jpg',
'jpeg'=>'image/jpeg',
'gif'=>'image/gif',
'png'=>'image/png',
);
if(isset($_POST) && !empty($_POST['check']))
{
// Is there really something there in $_FILES, is it an array? Is it empty?
if(!isset($_FILES) || !is_array($_FILES) || empty($_FILES))
{
showError('An error occured. There was no image uploaded.');
showForm();
exit;
}
// $_FILES array is populated, is it really an uploaded file?
if(!is_uploaded_file($_FILES['image']['tmp_name']))
{
showError('An error occurred during upload. Your image was not successfully uploaded. Please try again.');
showForm();
exit;
}
// Is the file uploaded of an allowed type?
if(!in_array($_FILES['image']['type'], $allowed_types))
{
showError('The image you uploaded is not one of the allowed image types. The allowed image types are:<br>'
.dispArray($allowed_types));
showForm();
exit;
}
echo 'Your Image:<br>';
displayPhoto();
}
showForm();
// Functions used:
function displayPhoto()
{
$type = $_FILES['image']['type'];
echo '
<img src="displayphoto.php?t='.urlencode($type).'&f='.urlencode($_FILES['image']['tmp_name']).'" alt="Uploaded Image" />
';
}
function dispArray($array)
{
echo '
<ul class="array">';
foreach($array as $key=>$val)
{
echo '
<li>'.humanType($key).'</li>
';
}
echo '
</ul>';
}
function humanType($type)
{
switch($type)
{
case 'jpg':
case 'jpeg':
return 'JPEG Image File [ .jpg or .jpeg ]';
case 'gif':
return 'GIF Image File [ .gif ]';
case 'png':
return 'PNG Image File [ .png ]';
}
}
function showError($message)
{
echo '
<div class="error">
<p>'
.$message
.'
</p>
</div>';
}
function showForm()
{
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<input type="hidden" name="check" value="aValue" />
Image: <input type="file" name="image" /><br />
<input type="submit" name="submit" value="Check" /><input type="reset" name="reset" value="Reset" />
</form>
<?php
showFooter();
}
function showFooter()
{
echo '
</body>
</html>';
}
?>
displayphoto.php
<?php
$file = urldecode($_GET['f']);
$type = urldecode($_GET['t']);
$contents = '';
$fp = fopen($file, "rb");
while(!feof($fp))
{
$contents .= fread($fp, 1024);
}
/*if(eregi("^.*nix.*$", $_ENV['OS']))
$contents = str_replace("\r", "\n", str_replace("\r\n", "\n", $contents));*/
fclose($fp);
header('Content-Type: '.$type);
print $contents;
?>
On linux servers it keeps giving me a broken image. Not sure why, but I'm workin on it....
Okay, so it seems that the URI string was too long. So I updated the script to send the tempname to the displayphoto.php page,
and it seems that won't work. Above is what I tried with my live linux server, and it just doesn't work.
THe error I get when I try to view the image directly is that the file doesn't exist.
So it seems that the there might be something wrong with the session, or PHP may
be deleting the file early.
So it would seem the only real way to do this is to move the image to one of your specified temporary directories in your webroot, and call it directly.
The file will be deleted from the temporary directory at the end of the request if it has not been moved away or renamed.
I'm not sure if this is the answer I've been looking for, but from that statement, you can't "stream" an image, at least not an uploaded one. Looks like you'll have to move it to a web-visible temporary directory (something like public_html/images/tmp) and use the normal image tag. Sorry.