【发布时间】:2013-02-03 02:55:21
【问题描述】:
好的,我知道有人问过这个问题,但我仍然无法弄清楚我的代码出了什么问题。
我正在尝试将图像上传到数据库并将其存储为 blob,以便它可以在页面上输出。一切正常,一切都存储在 mysql 数据库中,但是当我尝试回显 blob 时,它给了我一个损坏的图像。这是我的代码。
$file = $_FILES['image']['tmp_name'];
if(!isset($file)) {
echo "Please select image.";
} else {
$image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
$image_name = $_FILES['image']['name'];
$image_size = getimagesize($_FILES['image']['tmp_name']);
if($image_size==FALSE) {
echo 'that is not an image.';
} else {
if (!$insert = mysql_query("INSERT INTO photo VALUES ('', '$image_name', '$image')")) {
echo "Problem uploading image";
} else {
$lastid = mysql_insert_id();
echo "Image uploaded.<p />Your image:<p /><img src=ShowPics.php?id=$lastid>";
}
}
}
?>
<form action="Photosite.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image"></br></br>
<input type="submit" value="Submit">
</form>
还有我的 PHP 页面
<?php
$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM photo WHERE id=$id");
$image = mysql_fetch_assoc($image);
$image = $image['image'];
header("Content-type: image/jpeg");
echo $image;
?>
任何帮助表示赞赏???我只是不明白为什么我的图像损坏了......
【问题讨论】:
-
Please, don't use
mysql_*functions in new code。它们不再维护and are officially deprecated。看到red box?改为了解prepared statements,并使用PDO 或MySQLi - this article 将帮助您决定哪个。如果你选择 PDO,here is a good tutorial. -
谢谢,我知道这些,但还没有接触过。