I perfectly understand what you are trying to do.
What I mean is, the if/else block acts as two completely different pages. You cannot mix the two by putting browser output (HTML, etc) outside of the if/else block
The solution you posted works, except can make the file server messy with a bunch of temporary files. (You can put unlink at the bottom of your script. HTML will load it properly, then unlink will delete it and, since it's already loaded, it wont affect the page display. Just if they try to save it or go to it, it wont exist.)
So, this is a revamped example. This instead takes image data from a database (based on my table at the bottom):
<?php
//
// Lets you use an image tag to specify an image internally
// Displays image from database acting like a new page
//
$types = array('jpg', 'jpeg', 'gif', 'png', 'bmp');
$image = (eregi('^[a-z0-9_-]+\.(' . implode('|', $types) . ')$', $_GET['img'])) ? $_GET['img'] : '';
mysql_connect('localhost', 'username', 'password');
mysql_select_db('database');
if (isset($image) && !empty($image))
{
// This is an image page based on conditions of $_GET['img']
$result = mysql_query("SELECT * FROM images WHERE image_name = '$image'");
$row = mysql_fetch_assoc($result);
$type = strtolower(substr(strrchr($image, '.'), 1));
if ($row && in_array($type, $types))
{
header("Content-Type: $type");
echo $row['image_file'];
}
}
else
{
// This is the main default page
echo '<h1>HI THERE</h1>
<br /><img src="' . $_SERVER['PHP_SELF'] . '?img=image.jpg">';
}
/*
CREATE TABLE images (
image_id INT(3) NOT NULL AUTO_INCREMENT,
image_name VARCHAR(255) NOT NULL,
image_file TEXT,
PRIMARY KEY(image_id)
);
*/
?>
Do you understand what this does?
Edit: Here's an
example.