Hi All,
I'm going throught some tutorial about uploading and displaying
images files. But the display script isn't working. Here's what I have:
Table setup is:
CREATE TABLE `image` (
`ImageId` int(10) NOT NULL auto_increment,
`Image` longblob,
`FileType` varchar(32) default NULL,
PRIMARY KEY (`ImageId`)
)
Add image script (addimage.php) is:
<?php // add image script
if ($_POST['Submit']) {
if ($_POST['MAX_FILE_SIZE'] >= $_FILES['file']['size']) {
//print_r($_FILES);
include ('includes/mysql_connect.php'); // connect to db
$photo = addslashes(fread(fopen($_FILES['file']['tmp_name'], "r"),
$_FILES['file']['size']));
$query = sprintf("INSERT INTO image(Image, FileType) VALUES ('%s',
'%s')", $photo, $_FILES['file']['type']);
if (mysql_query($query)) {
$messages[] = "Your files is successfully store in database";
} else {
$messages[]= mysql_error();
}
} else {
$messages[] = "The file is bigger than the allowed size (96k) please
reduce your file size";
}
}
?>
<html>
<head>
<title>Add Image</title>
</head>
<body>
<?
if (isset($messages)) {
foreach ($messages as $message) {
print $message ."<br>";
}
}
?>
<form action="" method="post" enctype="multipart/form-data" name="form1">
<input type="file" name="file">
<input type="hidden" name="MAX_FILE_SIZE" value="96000">
<input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
And imageloader.php is:
<?php // imageloader.php
include ('includes/mysql_connect.php');
$result = mysql_query("SELECT ImageId from image");
while ($row = mysql_fetch_array($result)) {
$ids[]=$row['ImageId'];
}
?>
<html>
<head>
<title>Image Loader</title>
</head>
<body>
select image:<br>
<table width="80%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="10%">id</td>
<td width="90%">Image</td>
</tr>
<tr>
<td valign="top">
<table width="100%" border="0"
cellspacing="0" cellpadding="0">
<? foreach ($ids as $id) { ?>
<tr>
<td><a href="?id=<?= $id;
?>"><?= $id; ?></a></td>
</tr>
<? } ?>
</table>
</td>
<td><? if (isset($_GET['id'])) { ?><img
src="image.php?id=<?= $_GET['id']; ?>"><? } ?></td>
</tr>
</table>
</body>
</html>
And finally image.php is
<?php // image.php
include ('includes/mysql_connect.php'); // connect to db
$result = mysql_query(sprintf("SELECT * from image WHERE ImageId =
%d", $_GET['id']));
$row = mysql_fetch_array($result);
header(sprintf("Content-type: %s", $row['FileType']));
echo "Here's the picture: :", $row['Image'];
?>
When I click on the individual image id, the actual image won't
show. Can someone tell me what am I missing here?
Much thanks.
Kay