我在DB中插入图像时遇到问题.
该表具有以下结构:
> id-> INT(3)->自动增量
>名称-> VARCHAR(30)
> extension-> VARCHAR(10)[可能太短]
> img-> MEDIUMBLOB
插入图像的PHP代码为:
if($_FILES['file']['error']==0){
$result = is_uploaded_file($_FILES['file']['tmp_name']);
if(!$result){
echo "Upload failed";
}else{
$type = explode("/", $_FILES['file']['type']);
$extension = $type[1];
$name = $_FILES['file']['name'];
$img = $_FILES['file']['tmp_name'];
$img = file_get_contents($_FILES['file']['tmp_name']);
$img = addslashes ($img);
}
$sql = "INSERT INTO images (name, extension, img) VALUES ('$name', '$extension', '$img')";
$result = $mysqli->query($sql);
if($result){
echo "insertion was successful";
}else{
echo "insertion failed: ".$mysqli->error;
}
这就是我尝试查看img的方法:
$sql = "SELECT name, extension, img FROM images WHERE id='1'";
$result = $mysqli->query($sql);
if($result){
$a = $result->fetch_assoc();
header ("Content-type: image/".$a['estensione']);
echo $a['img'];
}else{
echo "AAAAAAAAA
";
echo $mysqli->error;
}
插入还可以,但是我无法查看图像.
另外,还有另一种在Db中上传图片的方法吗?