mysql数据库添加图像,向/从MySQL数据库插入/查看图像

I have a problem with inserting images in DB.

The table has the following structure:

id->INT(3)->autoincrement

name->VARCHAR(30)

extension->VARCHAR(10) [maybe too short]

img->MEDIUMBLOB

The PHP code that insert the image is:

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;

}

And this is how i try to see 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;

}

The insertion is Ok, but i can't view the image.

In addition, there's another way to upload image in Db?

解决方案

First in image tag example (the page where you want to display your results)

in the tag get the image from fetch_image_frm_db.php page, and display

fetch_image_frm_db.php?id=<?php%20echo%20%24id_of_row;?>

fetch_image_frm_db.php page

$id=$_GET['id'];

$query = "SELECT * FROM images WHERE id=$id";

$result=mysql_query($query) or die('Error, query failed'.mysql_error());

$row=mysql_fetch_array($result);

header("Content-type:image/jpeg");

stripslashes ($row['img']);

echo $row['img'];

** storing images in db is not good practise

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值