繁体   English   中英

从PHP引用图像

[英]referencing an image from php

我在网站上有一个功能,在该网站上有一个指向php页面的img参考

<img src="selectimage.php?uid=21312412">

我需要返回图像并更新数据库文件

<?php
$servername = "localhost";
$username = "webz";
$password = "BLAH";
$dbname = "contactlist";
$readid = $_GET["uid"];
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "UPDATE emails SET `read`='1' WHERE `id`='" .$readid. "'";
if ($conn->query($sql) === TRUE) {
echo "<img src='logo11w.png'>";
}

$conn->close();
?>

我认为这将无法正常工作,因为我正在引用其中引用HTML标记的php页面

虽然这正在更新我的数据库,但未显示图像。

我如何使此php文件将图像logo11w.png发送到该img标签

进一步说明:不能在页面加载中包含此代码的包含或其他方法,因为可能从非服务器源调用此过程。

将用户ID存储在一个隐藏字段中
更新数据库中的值后,您可以使用ajax从php页面获得回复

<input type="hidden" id="userid" value="user_id_value">
<input type="button" id="getimage" value="GET IMAGE">
<div id="u_img"></div>
$(document).ready(function(){
    $('#getimage').click(function() {
        var uid = $('#userid').val();
        var datastring = "uid="+uid;
        $.ajax({
            type: "POST",
            url: "php_page.php",
            data: datastring,
            cache: false,
            success: function(result) {
                $('#u_img').append(result);
            }
        });
    });
});

我尝试做的最有效的方法是:

...

if ($conn->query($sql) === TRUE) {
header('Content-Type: image/jpeg');
echo file_get_contents('https://www.google.com/images/srpr/logo11w.png'); 
} else {
echo "Error updating record: " . $conn->error;
}

...

暂无
暂无

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

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