不显示blob图像

时间:2014-04-17 05:45:05

标签: php mysql blob

我在显示图像时出现问题,该图像作为blob存储在数据库中。我在很多论坛上搜索过,但找不到任何答案。

我将代码分成两个文件。在index.php中,我插入图像并尝试从get_image.php文件中加载它。

这是index.php

<form action="index.php" method="POST" enctype="multipart/form-data">
                  File:
                  <input type="file" name="image"> <input type="submit" value="upload">
                </form>

                <?php 
                  $file = $_FILES['image']['tmp_name'];

                  if(!isset($file)){
                    echo "Please select an image.";
                  }

                  else{
                    $image_name = mysql_real_escape_string($_FILES['image']['name']);
                    $image = mysql_real_escape_string(file_get_contents($_FILES['image']['tmp_name']));
                    $imageType = mysql_real_escape_string($_FILES['image']['type']);
                    $image_size = getimagesize($_FILES['image']['tmp_name']);


                    if($image_size == FALSE){
                      echo "That's not an image";
                    }
                    else{
                      if (!$insert = mysql_query("UPDATE 'table_name' SET `image` = '$image' WHERE 'row_name' = '$student_id'")){                                                        
                        echo "Problem updating image";
                      }

                      else{                                                      
                        echo "<img src=get_image.php>";
                      }

                    }
                  }
                ?>

和get_image.php

<?php 

        $query = mysql_query("SELECT image FROM table_name WHERE student_id = 51686");
        while($row= mysql_fetch_assoc($query)){
            $imageData = $row['image'];
        }

        header("Content-type: image/png");
        echo $imageData;        
?>

任何人都可以帮忙。我的php版本是5.2在输出中有图像图标,但没有图像。如果我右键单击并尝试将其另存为图像,则它不是图像格式,而是保存为get_image.php。所以我不知道这是什么问题。

2 个答案:

答案 0 :(得分:0)

请尝试插入如下记录

 $tmpImageName=$_FILES['files']['tmp_name'];
 $handle      = fopen( $tmpImageName, 'r' );
 $content = fread( $handle, filesize( $tmpImageName ) );
 fclose($handle);

 $sql='insert into imagetest(image)values("'.mysql_escape_string($content).'")';

 mysql_query($sql) or die(mysql_error());

然后尝试显示像这里的图像

$res = mysql_query("SELECT * FROM imagetest"); 
 while ($row = mysql_fetch_assoc($res)) {
   echo "<img src=data:image/jpeg;base64," . (base64_encode(($row['image']))) . " >";
  }

答案 1 :(得分:0)

$pdo = new PDO('mysql:dbname=database_name;host=localhost', 'username', 'password',
        array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));

      $imageName = mysql_real_escape_string($_FILES["image"]["name"]);
      $imageData = file_get_contents($_FILES["image"]["tmp_name"]);
      $imageType = mysql_real_escape_string($_FILES["image"]["type"]);

      $stmt = $pdo->prepare('INSERT INTO image (name,image) VALUES (:name,:image)');
      $stmt->bindParam(':image', $imageData, PDO::PARAM_LOB);
      $stmt->bindParam(':name', $imageName);
      $stmt->execute(array('name' => $imageName, 'image' => $imageData));
      echo "Image Uploaded";


      $res = mysql_query("SELECT * FROM image "); 
      while ($row = mysql_fetch_assoc($res)) 
      {                                                  
        echo "<img src=data:image/jpeg;base64," . (base64_encode(($row['image']))) . " style='width:60px;height:60px;'>";
      }