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:
我在堆栈溢出时遇到过类似的问题,但我不明白答案,所以我想,我会自己发布问题并与任何有用的程序员通信。我正在尝试显示存储在数据库中的图像,并且我得到了一个损坏的图像链接。我有两个php文件如下:
<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文件如下:
<?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)
);
3 个解决方案
#1
1
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.
检查输出图像的php文件的开头是否没有unicode BOM。
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: .
BOM在非unicode(hex-)编辑器中看起来像这样:
#2
1
Probably the culprit is mysql_real_escape_string()
.
可能罪魁祸首是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).
另外,请记住,BLOB MySQL数据类型的限制为64KB,由于base64编码通胀,它表示真正的文件限制为~48KB - 如果您尝试上传更大的文件,它将被截断并且它之后会显示一个损坏的文件(=什么都不显示)。
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;
}
?>
#3
0
if you are using chrome right click on the broken image and inspect the element and see what is wrong with the code.
如果您正在使用chrome,请右键单击损坏的图像并检查元素,看看代码有什么问题。
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
你有数据库行里面的img src的html吗?如果是这样浪费它在那里它应该在PHP文件中,如果你没有那么那就是你的问题
#1
1
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.
检查输出图像的php文件的开头是否没有unicode BOM。
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: .
BOM在非unicode(hex-)编辑器中看起来像这样:
#2
1
Probably the culprit is mysql_real_escape_string()
.
可能罪魁祸首是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).
另外,请记住,BLOB MySQL数据类型的限制为64KB,由于base64编码通胀,它表示真正的文件限制为~48KB - 如果您尝试上传更大的文件,它将被截断并且它之后会显示一个损坏的文件(=什么都不显示)。
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;
}
?>
#3
0
if you are using chrome right click on the broken image and inspect the element and see what is wrong with the code.
如果您正在使用chrome,请右键单击损坏的图像并检查元素,看看代码有什么问题。
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
你有数据库行里面的img src的html吗?如果是这样浪费它在那里它应该在PHP文件中,如果你没有那么那就是你的问题