【问题标题】:Saving image to mySQL db and printing it将图像保存到 mySQL 数据库并打印
【发布时间】:2012-09-05 18:43:15
【问题描述】:

要保存我正在使用的图像:

$image_bin = addslashes(file_get_contents($img_url));
mysql_query('INSERT INTO images
        (image_filename, image_type, image_bin)
    VALUES
        ("file.jpg", "image/jpeg", "'.$image_bin.'")');

显示:

$qry = 'SELECT image_filename, image_type, image_bin
    FROM images
    WHERE image_id = 1';
$result = mysql_query($qry);
$row = mysql_fetch_row($result);
header('Content-type: '.$row['image_type']);
echo stripslashes($row['image_bin']);

图像已保存到数据库,但图像尺寸变大。 并且图像颜色变得有些损坏。

例如打开开发者工具的 Chrome 中的图片截图: screensho url

有人知道我做错了什么吗?

【问题讨论】:

  • 尝试使用mysql_real_escape_string 而不是addslashes
  • @halfer 确实,查询中应该有一些 proper 转义。但我认为这不是问题的原因。
  • 菲利普,结果是一样的。对我来说奇怪的是,当我使用相同的方法上传文件时,一切都很顺利:/
  • 如果出现转义问题,则图像根本不会打印

标签: php mysql image escaping store


【解决方案1】:

试试……

//use mysql real escape
$image_bin = mysql_real_escape_string(file_get_contents($img_url));
mysql_query('INSERT INTO ' . PREFIX . 'news_images
    (image_filename, image_type, image_bin)
    VALUES
    ("file.jpg", "image/jpeg", "' . $image_bin . '")');


$qry = 'SELECT image_filename, image_type, image_bin
    FROM images
    WHERE image_id = 1';
$result = mysql_query($qry);
$row = mysql_fetch_row($result);
header('Content-type: ' . $row['image_type']);
echo $row['image_bin']; //no stripslashes

【讨论】:

  • 谁在现场。 Stripslashes 把一切都搞砸了。谢谢
猜你喜欢
  • 2014-09-05
  • 1970-01-01
  • 2011-02-14
  • 1970-01-01
  • 2023-03-15
  • 2017-04-26
相关资源
最近更新 更多