0

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

现在多数民众赞成在清除

我的问题是我通过输入将图像插入数据库。

(不要介意类安全调用,所做的只是检查数据是否有效)

$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'];
}
4

1 回答 1

7

使用addslashes是极不正确的。根据您的列是 TEXT 字段还是 BLOB 字段,您应该使用 Base64 或mysql_real_escape_string.

使用 Base64 并不难;你也可以使用这种方式。只需用 替换addslashesbase64_encode回显图像base64_decode

就此而言,有一种更简单的方法可以编写整个内容:

// begin upload
if ($imgsize > 0)
{
  $content = file_get_content($tmpname);
  $content = base64_encode($content);
}

然后输出你真的只需要做

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

但是,如果列是 BLOB,则可以直接使用mysql_real_escape_string

// begin upload
if ($imgsize > 0)
{
  $content = file_get_content($tmpname);
  $content = mysql_real_escape_string($content);
}

接着:

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

尽管从您当前的症状来看,我猜您还有一个与您的图像如何从数据库中存储和调用有关的错误,我需要查看您进行查询插入和调用的代码部分在我可以帮助您修复该部分之前从数据库中读取。


您当前的代码似乎大部分都很好。几个问题:

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

print $row['img'];

您可能打算摆脱第二行。此外,您应该使用echo而不是print; 每个人都使用它,它有时会稍微快一点,并且print除了返回一个值之外并没有任何好处:

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

$security->secure()似乎是某种消毒功能。只需使用mysql_real_escape_string()- 那是您应该使用的。除了$imgsize; 你可能想用intval()那个,因为你知道它应该是一个整数。

也在这里:

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

您将表格命名为tbl_schoolgallery上面几行。我假设$tbl == 'tbl_schoolgallery',但为了保持一致性,您应该$tbl在两个地方或tbl_schoolgallery两个地方都使用。

另外,将其替换whileif- 无论如何,如果它多次循环,您的代码将导致麻烦。

于 2009-06-26T06:03:06.980 回答