来自Blob数据PHP的图像损坏? [英] Broken Image from blob data PHP?

查看:83
本文介绍了来自Blob数据PHP的图像损坏?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我知道有人问过这个问题,但是我仍然无法弄清楚我的代码出了什么问题.

Ok so I know this question has been asked but I still could not figure out what was wrong with my code.

我正在尝试将图像上传到数据库并将其存储为Blob,以便可以在页面上输出.一切正常,一切都存储在mysql数据库中,但是当我尝试回显Blob时,它给了我一个损坏的图像.这是我的代码.

I am trying to upload an image to a database and store it as a blob so it can output on the page. Everything works, everything is stored in the mysql database but when I try to echo out the blob it gives me a broken image. Here is my code.

$file = $_FILES['image']['tmp_name'];

if(!isset($file)) {
echo "Please select image.";
} else {
    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = $_FILES['image']['name'];
    $image_size = getimagesize($_FILES['image']['tmp_name']);

    if($image_size==FALSE) {
    echo 'that is not an image.';
    } else {
     if (!$insert = mysql_query("INSERT INTO photo VALUES ('', '$image_name', '$image')")) {
      echo "Problem uploading image";
     } else {
      $lastid = mysql_insert_id();
      echo "Image uploaded.<p />Your image:<p /><img src=ShowPics.php?id=$lastid>";
     }
    }

}



?>


<form action="Photosite.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image"></br></br>
<input type="submit" value="Submit">
</form>

还有我的PHP页面

 <?php

 $id = addslashes($_REQUEST['id']);
 $image = mysql_query("SELECT * FROM photo WHERE id=$id");
 $image = mysql_fetch_assoc($image);
 $image = $image['image'];


 header("Content-type: image/jpeg");
 echo $image;

?>

任何帮助表示赞赏???我只是不明白为什么我的图像坏了...

Any help appreciated??? I just do not get why i am getting a broken image...

推荐答案

您在<?php之前的第一行中有一个空格.如果在您的代码中是这样的话,就会出现问题.在回显图像之前,不应输出任何内容.

You have a space on the first line, before <?php. If it's like that in your code, that gives the problem. Nothing should be outputted before you echo the image.

如果确定当前文件正确,则可以

If you're sure that the current file is correct you can do

<?php
ob_start();

// do stuff

ob_end_clean();
header("Content-type: image/jpeg");
echo $image;

使用此解决方法,如果de DB中的斑点正确,则会显示该图像.

With this workaround, the image is show if the blob in de DB is correct.

您可以使用转换在 phpmyadmin中检查BLOB.

You can check the BLOB in phpmyadmin using transformations.

P.S. addslashes仅在将值放在斜线之间时才有用,所以

P.S. addslashes only helps if you put the value between slashes, so

$id = addslashes($_REQUEST['id']);
$image = mysql_query("SELECT * FROM photo WHERE id='$id'");

这篇关于来自Blob数据PHP的图像损坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆