简体   繁体   English

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

[英]How to call php in img src tag?

This is the piece of my code from index.php: 这是我的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>

This is the imageView.php: 这是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);
?>

And this is the result: 结果如下:

在此处输入图片说明

With php/imageView.php?ID=<?php echo $row["ID"] I getting the path of my file: 使用php/imageView.php?ID=<?php echo $row["ID"]我得到了文件的路径:

uploads/film/pic/5.jpg

Why it doesn't displays the image? 为什么不显示图像?

Why it doesn't displays the image? 为什么不显示图像?

Because php/imageView.php?ID=<?php echo $row["ID"];?> returns the path of an image and doesn't return the image itself. 因为php/imageView.php?ID=<?php echo $row["ID"];?>返回图像的路径,而不返回图像本身。

imageView.php can take that path and redirect the user agent to it: imageView.php可以采用该路径并将用户代理重定向到该路径:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM