【问题标题】:correct way to upload image to database将图像上传到数据库的正确方法
【发布时间】:2010-11-06 01:28:18
【问题描述】:

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

现在已经清除了

我的问题是我通过键入此内容将图像插入到数据库中。

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

$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

当打开页面查看带有正确参数设置的图像时。


对于那些想要查看正在执行的查询以保存图像等的人 我复制了整个部分

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

【问题讨论】:

  • 你不应该使用 print $content 吗?
  • 我不能,因为我从数据库调用图像
  • 那部分代码和一个数据样本丢失了,你确定img列包含正确的值吗?
  • Content-Disposition 用于设置下载的文件名(例如,如果您正在向用户发送 Word 文档并在他们的 Save As... 提示出现时想要一个默认值)。如果在这种情况下正确使用,它将使其直接转到图像 URL 将强制下载而不是显示。
  • 虽然在 SQL 数据库中存储二进制数据存在问题,但如果您关心事务(尝试将文件系统与 sql 后端同步)、可伸缩性(大量工具可以帮助RDMS)、备份(为什么要编写/使用另一个备份系统?),以及尤其是所有这些问题的相互作用。

标签: php database blob


【解决方案1】:

使用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);
}

然后输出你真的只需要做

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

另外,将 while 替换为 if - 无论如何,如果它多次循环,您的代码将导致问题。

【讨论】:

  • 我添加了代码块:p 你可以查看它吗?谢谢你的时间。
  • 我感觉这就是错误所在:$tmpname = $security->secure($_FILES['imgschool']['tmp_name']);该类正在做的是通过 mysql_real_escape_string 和 htmlencode 传递一个值。所以临时图像文件正在经历破坏它的东西哈哈。有趣的是我使用了第 3 方数据库查看器,并且我能够清楚地看到数据库内的图像。
  • 是的,您不需要保护 $tmpname。
猜你喜欢
  • 1970-01-01
  • 2017-04-27
  • 2016-08-28
  • 1970-01-01
  • 2020-10-06
  • 1970-01-01
  • 1970-01-01
  • 2012-12-17
相关资源
最近更新 更多