php怎样上传到数据库,php – 将图像上传到数据库的正确方法

我知道你们中的一些人会说这不是正确的方法,但我在一个紧迫的截止日期前完成一个应用程序,截至目前我无法返回并修改代码将图像存储在目录中.

现在已经清除了

我的问题是我输入一个图像插入数据库.

(不介意类安全调用,如果数据有效,所有正在进行的操作都会进行一些检查)

$filename = $security->secure($_FILES['imgschool']['name']);

$tmpname = $security->secure($_FILES['imgschool']['tmp_name']);

$imgsize = $security->secure($_FILES['imgschool']['size']);

$imgtype = $security->secure($_FILES['imgschool']['type']);

$school = $security->secure($_POST['school']);

//begin upload

if($imgsize > 0) {

$handle = fopen($tmpname, "r");

$content = fread($handle, filesize($tmpname));

$content = addslashes($content);

//code to add all this to database

}

变量$content是图像,它的所有内容都是addslashes.我记得有人曾经提到过用base64做的事情,但我几乎无法回想起它是如何写的.

这就是我从数据库中调用图像的方式

除了所有的疑问和诸如此类的东西

这是调用图像的主要部分

header("Content-length: ".$imgsize);

header("Content-type: ".$imgtype);

header("Content-Disposition: attachment; filename=".$imgname);

print $row['img'];

我遇到的问题是,而不是图像显示.网址只显示,所以在这种情况下我只看到这个

打开页面时,使用url中设置的正确参数查看图像.

对于那些想要查看正在进行的查询以保存图像等的人

我复制了整个部分

//save image to db

if(isset($_POST['btnupload'])) {

$filename = $security->secure($_FILES['imgschool']['name']);

$tmpname = $security->secure($_FILES['imgschool']['tmp_name']);

$imgsize = $security->secure($_FILES['imgschool']['size']);

$imgtype = $security->secure($_FILES['imgschool']['type']);

$school = $security->secure($_POST['school']);

//begin upload

if($imgsize > 0) {

$handle = fopen($tmpname, "r");

$content = fread($handle, filesize($tmpname));

$content = base64_encode($content);

}

$save = mysql_query("insert into tbl_schoolgallery(id,hash,img,imgtype,imgsize) values(null,'$school','$content','$imgtype','$imgsize')") or die(mysql_error());

header("Location: school-catalog.php?page=school_gallery");

}

//call image from db

$query = mysql_query("select * from $tbl where id = '$id'") or die(mysql_error());

while($row = mysql_fetch_assoc($query)) {

$imgtypeget = explode("/", $row['imgtype']);

$imgname = "img.".$imgtypeget[1];

$imgtype = $row['imgtype'];

$imgsize = $row['imgsize'];

header("Content-length: ".$imgsize);

header("Content-type: ".$imgtype);

print base64_decode($row['img']);

print $row['img'];

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值