Reputation: 6515
I have seen similar questions here on stack overflow but I didn't understand the answers so I thought, I'd post the question myself and correspond with any of the helpful programmers. I'm trying to display an image which is stored in a database and I am getting a broken image link. I have two php files as follows:
<html>
<head>
<title>Upload an image</title>
</head>
<body>
<form action="image_practice.php" method="POST" enctype="multipart/form-data">
Image: <input type="file" name="image"> <input type="submit" value="Upload Image">
</form>
<?php
if (function_exists('hex2bin') !== true)
{
function hex2bin($data)
{
return pack("H*", $data);
}
}
//file properties
$file= $_FILES['image']['tmp_name'];
if (!isset($file))
echo "Please select an image";
else{
$image = bin2hex(file_get_contents($_FILES['image']['tmp_name']));
$image = hex2bin($image['Image']);
if ($image_size==FALSE)
echo "That's not an image";
else {
if(!$insert = mysql_query("INSERT INTO ImageDetails VALUES('','$image_name','$image')"))
echo "Problem Uploading image.";
else {
$lastid= mysql_insert_id();
echo "Image Uploaded.<p />Your Image:<p \><img src='GetImage_prac.php?id=$lastid'>";
}
}
}
?>
</body>
</html>
and then another php file which displays the image as follows:
<?php
if (function_exists('hex2bin') !== true)
{
function hex2bin($data)
{
return pack("H*", $data);
}
}$id=addslashes($_REQUEST['id']);
$image= mysql_query("SELECT * FROM ImageDetails WHERE id=$id");
$image= mysql_fetch_assoc($image);
$image= $image['Image'];
header("Content-type: image/jpeg");
echo $image;
?>
I don't understand why the image isn't displaying. Can anyone help? I think the table definition is as follows:
CREATE TABLE ImageDetails2
(
ImageId int NOT NULL AUTO_INCREMENT,
Name varchar(30) NOT NULL,
Image BLOB,
PRIMARY KEY(ImageId)
);
Upvotes: 1
Views: 2396
Reputation: 154691
Probably the culprit is mysql_real_escape_string()
.
Also, bare in mind that the BLOB
MySQL data type has a limit of 64KB, which, because of the base64 encoding inflation, represents a true file limit of ~48KB - if you try to upload a bigger file it'll get truncated and it will display a corrupted file afterwards (= display nothing).
Okay, I re-did your whole code, the problem was the query in the display script:
SELECT * FROM ImageDetails WHERE id=$id
Should be:
SELECT * FROM ImageDetails WHERE ImageId=$id
Anyway, here it is, all fixed and improved. It has to work now:
<html>
<head>
<title>Upload an image</title>
</head>
<body>
<form action="image_practice.php" method="POST" enctype="multipart/form-data">
<label for="image">Image:</label>
<input type="file" name="image" id="image">
<input type="submit" value="Upload Image">
</form>
<?php
if ((strcasecmp('POST', $_SERVER['REQUEST_METHOD']) === 0) && (isset($_FILES) === true))
{
if (exif_imagetype($_FILES['image']['tmp_name']) != false)
{
$image = file_get_contents($_FILES['image']['tmp_name']);
$image_name = mysql_real_escape_string($_FILES['image']['name']);
$image = base64_encode($image);
if (mysql_query("INSERT INTO ImageDetails VALUES ('', '$image_name', '$image');") !== false)
{
echo 'Image Uploaded.<p />Your Image:<p \><img src="GetImage_prac.php?id=' . mysql_insert_id() . '">';
}
else
{
echo "Problem Uploading image.";
}
}
else
{
echo "That's not an image.";
}
}
?>
</body>
</html>
<?php
// connect to MySQL here
if (array_key_exists('id', $_REQUEST) === true)
{
$query = mysql_query("SELECT * FROM ImageDetails WHERE ImageId = " . intval($_REQUEST['id']) . " LIMIT 1;"); # you had "WHERE id" here!
$result = mysql_fetch_assoc($query);
if ($result !== false)
{
$image = $result['Image'];
$image = base64_decode($image);
}
}
if (isset($image) === true)
{
header('Content-type: image/jpeg'); echo $image;
}
else # display a 1x1 spacer GIF as fallback
{
$image = base64_decode('R0lGODlhAQABAID/AMDAwAAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==');
header('Content-type: image/gif'); echo $image;
}
?>
Upvotes: 1
Reputation: 5878
This is a shot at something that may cause the error, but it can be an other:
Check that there is no unicode BOM at the start of the php file that outputs the image.
To do this (and even more), save the image when the browser presents the error, rename it to text, or look at it using a hex editor and compare that with the initial image.
A BOM looks like this in a non-unicode (hex-)editor: .
Upvotes: 1
Reputation: 3290
if you are using chrome right click on the broken image and inspect the element and see what is wrong with the code.
Do you have the html for the img src inside the database rows? if so its a waste having it in there it should be in the php file and if you dont then thats your problem
Upvotes: 0