如何在img src标签中调用php?

时间:2016-11-12 16:08:55

标签: php image display

这是我的代码来自index.php:

<div class="row">
<?php 
    $conn = mysql_connect("localhost","root","");
    mysql_select_db("imdb_db");
    $res = mysql_query("select * from `film` ORDER BY ID DESC");
    while($row = mysql_fetch_array($res)) {
        ?><div class='col-xs-2'><?php
        ?><p style="position:relative;left:-40px"><?php echo $row["Cim"];?></p><?php
        ?> <img src="php/imageView.php?ID=<?php echo $row["ID"];?>" class="img-responsive" style="width:200px;height:250px;" alt="Image"> <?php 
        ?></div><?php
    }
    mysql_close($conn);
?>
</div>

这是imageView.php:

<?php
    $conn = mysql_connect("localhost","root","");
    mysql_select_db("imdb_db") or die(mysql_error());
    if(isset($_GET['ID'])) {
        $sql = "SELECT `Boritokep` FROM `film` WHERE ID=". $_GET['ID'];
        $result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>" . mysql_error());
        $row = mysql_fetch_array($result);
        echo $row["Boritokep"];
    }

    mysql_close($conn);
?>

这就是结果:

enter image description here

使用php/imageView.php?ID=<?php echo $row["ID"]我获取文件的路径:

uploads/film/pic/5.jpg

为什么它不显示图像?

1 个答案:

答案 0 :(得分:2)

  

为什么它不显示图像?

因为php/imageView.php?ID=<?php echo $row["ID"];?>返回图像的路径而不返回图像本身。

imageView.php可以采用该路径并将用户代理重定向到该路径:

header(sprintf('Location: %s', $row["Boritokep"]));