Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/247.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从MySQL服务器解码Base64图像_Php_Mysql_Encoding_Base64 - Fatal编程技术网

Php 从MySQL服务器解码Base64图像

Php 从MySQL服务器解码Base64图像,php,mysql,encoding,base64,Php,Mysql,Encoding,Base64,通过使用Base64对图像进行编码,我将图像作为BLOB存储在在线MySQL数据库中。我没有存钱的问题。但我无法从服务器检索图像。它们似乎坏了。我相信这是因为它没有被解码 我尝试手动将几张照片上传到服务器上,由于它们没有编码,因此被正确检索。这是我用来检索图像的代码。有人能告诉我怎么解码这个图像吗 <?php $db = mysql_connect("localhost","un","pw") or die(mysql_error()); mysql_select_db("datab",

通过使用Base64对图像进行编码,我将图像作为BLOB存储在在线MySQL数据库中。我没有存钱的问题。但我无法从服务器检索图像。它们似乎坏了。我相信这是因为它没有被解码

我尝试手动将几张照片上传到服务器上,由于它们没有编码,因此被正确检索。这是我用来检索图像的代码。有人能告诉我怎么解码这个图像吗

<?php
$db = mysql_connect("localhost","un","pw") or die(mysql_error()); 
mysql_select_db("datab",$db) or die(mysql_error()); 
$userId = $_GET['eid']; 
$query = "SELECT image FROM event WHERE eid='$userId'"; 
$result = mysql_query($query) or die(mysql_error()); 
$photo = mysql_fetch_array($result); 
header('Content-Type:image/png;base64'); 
echo $photo['image']; 
?>

首先,请注意mysql语法已经过时,完全不推荐使用!请改用mysqli或PDO

然后,按照代码,只需在html文件中调用图像,如下所示:

<img src="data:image/png;base64, <?php echo $photo['image']; ?>">
“>
  • 将您升级到mysqli,并向您展示如何准备和执行语句。(完全公开,我已经很长时间没有编写mysqli代码了,所以它可能是错误的)
  • 如果图像不存在,则发送404 not found状态并终止脚本。否则图像为
    base64\u decode
    'd并输出到浏览器


  • Base64不是一种加密算法。它是一种编码。要进行加密,您需要某种默认Base64缺少的密钥。好吧,我的错。谢谢您指出。无论如何,我如何解码?@ArtjomB。从解决手头的实际问题开始。如果您不解决它,SQL注入将给您带来很多麻烦。@Darren,哪一个你在说什么?很抱歉,我是一个初学者。你能告诉我一种解码图像并显示它的方法吗?你的代码
    从事件中选择图像,其中eid='$userId'
    是可注射的。你需要清理
    $userId
    。实际上,我想用
    服务器/image.php?eid=351而不是HTML来测试它。所以我猜什么时候选择本身,图像应该被解码谢谢一堆的答案。顺便问一下,回声图像的格式是什么?我正在让图像显示在浏览器上,但我的最终目标是让这个图像显示在我的android应用程序上。在那里,我使用HTTP URL连接并将流解码为位图。我想知道代码回显的流的格式是。存储值是什么格式?.bmp?,.jpg?它是按base64编码的还是按原样存储的?
    
    $db = new mysqli( 'localhost' , 'un' , 'pw', 'datab' );
    $userId = intval( $_GET['eid'] ); //convert it to an int.
    $stmt = $db->prepare( 'SELECT image FROM event WHERE eid=? LIMIT 1' ); //prepare the statement
    $stmt->bind_param( 'i',$userId ); //bind our parameter to the statement
    $stmt->execute(); //execute statement
    $stmt->bind_result( $img ); //were selecting 1 cell (1 column of 1 row), so we can just bind the result to a single var with this line.
    $stmt->store_result(); //store our result, so we can see how many rows it returned.
    if($stmt->num_rows !== 1){
        http_response_code( 404 ); //image doesnt exist; send 404 status and die.
        die;
    }else{
        $stmt->fetch(); //fetch the result of the statement. this populates `$img` for us
        $stmt->close(); //close the prepared statement. 
        header( 'Content-Type: image/png' ); 
        echo base64_decode( $img ); //base64 decode image and echo it.
    }