我知道你们中的一些人会说这不是正确的方法,但我在一个紧迫的截止日期前完成一个应用程序,截至目前我无法返回并修改代码将图像存储在目录中.
现在已经清除了
我的问题是我输入一个图像插入数据库.
(不介意类安全调用,如果数据有效,所有正在进行的操作都会进行一些检查)
$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
}
Run Code Online (Sandbox Code Playgroud)
变量$ content是图像,它的所有内容都是addslashes.我记得有人曾经提到过用base64做的事,但我几乎无法回想起它是如何写的.
这就是我从数据库调用图像的方式
除了所有的查询,这是调用图像的主要部分
header("Content-length: ".$imgsize);
header("Content-type: ".$imgtype);
header("Content-Disposition: attachment; filename=".$imgname);
print $row['img'];
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,而不是图像显示.网址只显示,所以在这种情况下我只看到这个
HTTP://localhost/admin/school-catalog.php页=图库&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'];
}
Run Code Online (Sandbox Code Playgroud)
使用addslashes非常不正确.根据您的列是TEXT字段还是BLOB字段,您应该使用Base64或mysql_real_escape_string.
使用Base64并不难; 你也可以这样使用.只需更换addslashes与base64_encode和回声图像base64_decode.
对于这个问题,有一个更简单的方法来编写整个事情:
// begin upload
if ($imgsize > 0)
{
$content = file_get_content($tmpname);
$content = base64_encode($content);
}
Run Code Online (Sandbox Code Playgroud)
然后输出你真的只需要做
header("Content-type: ".$imgtype);
echo base64_decode($img);
Run Code Online (Sandbox Code Playgroud)
但是,如果列是BLOB,则可以直接使用mysql_real_escape_string:
// begin upload
if ($imgsize > 0)
{
$content = file_get_content($tmpname);
$content = mysql_real_escape_string($content);
}
Run Code Online (Sandbox Code Playgroud)
然后:
header("Content-type: ".$imgtype);
echo $img;
Run Code Online (Sandbox Code Playgroud)
从你目前的症状判断,我猜你也有一个关于如何从数据库中存储和调用你的图像的错误,我需要看到你要插入查询的代码部分和在我帮助您修复该部分之前从数据库中读取.
您当前的代码似乎很好.一些问题:
print base64_decode($row['img']);
print $row['img'];
Run Code Online (Sandbox Code Playgroud)
你可能想要摆脱第二排.此外,你应该使用echo而不是print; 每个人都使用它,它有时会稍微快一些,并且print除了返回一个值之外没有任何其他好处:
echo base64_decode($row['img']);
Run Code Online (Sandbox Code Playgroud)
$security->secure()似乎是某种消毒功能.只是使用mysql_real_escape_string()- 这是你应该使用的那个.除外$imgsize; 你可能想intval()在那个上使用,因为你知道它应该是一个整数.
也在这里:
$query = mysql_query("select * from $tbl where id = '$id'") or die(mysql_error());
Run Code Online (Sandbox Code Playgroud)
您将表格命名为tbl_schoolgallery上面几行.我假设$tbl == 'tbl_schoolgallery',但为了保持一致性,你应该$tbl在两个地方或tbl_schoolgallery两个地方使用.
此外,更换while有if-你的代码会造成麻烦,如果它曾经循环不止一次,反正.