dongwupei7803 2014-04-17 05:45
浏览 169
已采纳

不显示blob图像

I'm having problem with showing image which is stored in database as a blob. I searched in many forums, but couldn't find any answer.

I separated the code into two files. In index.php i insert image and try to load it from get_image.php file.

Here is 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>";
                      }

                    }
                  }
                ?>

And 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;        
?>

Can anyone help please. My php version is 5.2 At the output there is image icon, but no image. If I right click and try to save it as image, it's not an image format, it's saved as get_image.php. So I don't know what is the problem.

  • 写回答

2条回答 默认 最新

  • ds34222 2014-04-24 08:25
    关注
    $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;'>";
          }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决