我知道你们中的一些人会说这不是正确的方法,但我在紧迫的期限内完成应用程序,截至目前我无法返回并修改代码以将图像存储在目录中。
现在多数民众赞成在清除
我的问题是我通过输入将图像插入数据库。
(不要介意类安全调用,所做的只是检查数据是否有效)
$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 是图像,它所得到的只是添加斜杠。我记得有人曾经提到过用一种叫做 base64 的东西来做这件事,但我几乎记不起它是怎么写的了。
这就是我从数据库中调用图像的方式
除了所有查询之外,这是调用图像的主要部分
header("Content-length: ".$imgsize);
header("Content-type: ".$imgtype);
header("Content-Disposition: attachment; filename=".$imgname);
print $row['img'];
我遇到的问题是,而不是图像显示。网址只显示,所以在这种情况下我只看到这个
http://localhost/admin/school-catalog.php?page=gallery&id=4
打开页面以查看带有在 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'];
}