首页 > 解决方案 > 使用 PHP 不显示图像

问题描述

我正在尝试displaydatabase. 以下query工作正常,但问题image不再显示。

<?php
session_start();

// Include config file
require_once "../auth/dbconnection.php";

$output='';
    
$sql='select `image`,`title`,`sub_title`,`username`,`blog_id`,`body`,`published` from `blog` WHERE user_id=? ';

$stmt=$conn->prepare( $sql );
$stmt->bind_param( 's',$_SESSION['user_id'] );

$res=$stmt->execute();
if( $res ){
    $stmt->store_result();
    $stmt->bind_result($image,$title,$sub_title, $username, $blog_id,$body,$published);

      while( $stmt->fetch() ){
          
       $filepath="../assets/img/blog_images/";

        $title= substr($title,0,30);
        $body= substr($body,0,500);
             
        $date=   date('dS F Y', strtotime($published));
      
$output .=  '
<div class="col-sm-6 col-md-6 col-lg-4">
<div class="blog grid-blog">
<div class="blog-image">
<a href="#">';
$output .=  ' <img style="height:190px; width:330px;" class="img-fluid" src="data:image/png;base64, %s" alt="" />'; base64_encode(file_get_contents($filepath.$image) ) ;     
       
$output .=  ' </a>
</div>
<div class="blog-content">
<h3 class="blog-title"><a href="blog-details.html"> '.$title.' </a></h3>
<p>  <code>  '.$body.'  </code>  </p> <br>
<a href="blog-details.html" class="read-more"><i class="fa fa-long-arrow-right"></i> Read More</a>
<div class="blog-info clearfix">
<div class="post-left">
  <ul>
     <li><a href="#."><i class="fa fa-calendar"></i> <span>'.$date.'</span></a></li>
  </ul>
</div>
<div class="post-right"><a href="#."><i class="fa fa-heart-o"></i>21</a> <a href="#."><i class="fa fa-eye"></i>8</a> <a href="#."><i class="fa fa-comment-o"></i>17</a></div>
</div>
</div>
</div>
</div>';

}

echo $output;

    }else{

        echo 'No any post found';
    }     
?>

谁能指导我如何解决这个问题,如果有人指导我解决这个问题,我将不胜感激。谢谢你。

标签: php

解决方案


我假设您省略了sprintf功能。这应该有效。

sprintf('<img src="data:image/png;base64, %s" alt="" />', base64_encode(file_get_contents($filepath.$image));     

推荐阅读