如何显示blob图像

时间:2016-05-09 11:45:36

标签: php mysql

我正在尝试插入&显示多个blob图像,但由于一些错误,它没有出现在页面上,多个图像也没有保存,代码如下: test1.php [插入blob图像]:

$conn  = mysqli_connect("localhost", "root", "root", "test");

for($i=0; $i<count($_FILES["userfile"]["name"]); $i++){

    $imageName = mysqli_real_escape_string($conn, $_FILES["userfile"]["name"]);
    $imageData = mysqli_real_escape_string($conn, $_FILES["userfile"]["tmp_name"]);
    $imageType = mysqli_real_escape_string($conn, $_FILES["userfile"]["type"]);
    $image     = file_get_contents($imageData);
    $image     = base64_encode($image);
    if($imageName!=''){

        mysqli_query($conn, "INSERT INTO files SET mime = '".$imageName."', data= '".$image."' ");
        $result =   array("insert_res"=>"110");
        echo json_encode($result);

    }else{
        $result =   array("insert_res"=>"111");
        echo json_encode($result);
    }
}

?>
<!DOCTYPE html>
<html>
    <head>
        <title>Image Upload</title>
    </head>
    <body>
<!--        <form name="" action="<?php echo $_SERVER["PHP_SELF"]; ?>" id="" method="POST" enctype="multipart/form-data">
            <input type="file" name="image" id="image" />
            <input type="submit" name="submit" value="Upload" />-->
             <img src="test_dis.php?id=1" />
        <!--</form>-->

    </body>
</html>

test_disp.php [显示数据库中的图像]:

$conn  = mysqli_connect("localhost", "root", "root", "test");


if(isset($_GET['id'])){
$id = mysqli_real_escape_string($conn, $_GET['id']);

$sql       = "SELECT * FROM files WHERE id = '".$id."' ";
$result    = mysqli_query($conn, $sql);
While($row = mysqli_fetch_assoc($result)){
$imageData = $row[data];
}
header("contetnt-type: image/jpg");
echo $imageData;
}
else{
    echo 'error';
}

对于单个图像,它的工作正常,但是当有多个图像时,它不会全部插入而且也不会取出。 我从IOS应用程序获取这些所有图像。 怎么解决呢?请高兴 同时展示它的来历。 enter image description here

enter image description here  插入其重复的第一个图像三次,但从IOS应用程序,我只获得3个图像,它跳过一个图像

enter image description here

1 个答案:

答案 0 :(得分:1)

首先,您没有正确地遍历$FILES数组。您需要做的就是将$i循环计数器添加到$FILES....引用

$conn  = mysqli_connect("localhost", "root", "root", "test");

for($i=0; $i<count($_FILES["userfile"]["name"]); $i++){

    $imageName = mysqli_real_escape_string($conn, $_FILES["userfile"]["name"][$i]);
    $imageData = mysqli_real_escape_string($conn, $_FILES["userfile"]["tmp_name"][$i]);
    $imageType = mysqli_real_escape_string($conn, $_FILES["userfile"]["type"][$i]);
    $image     = file_get_contents($imageData);
    $image     = base64_encode($image);
    if($imageName!=''){

        $res = mysqli_query($conn, "INSERT INTO files 
                                    SET mime = '$imageName', 
                                        data= '$image'");

        if ( $res === false ) {
            // check insert worked, reply with error 112 if not
            $e = mysqli_error($conn);
            echo json_encode( array(
                                    'insert_res'=>'112'
                                    'insert_error' => $e
                                   )
                            );
        } else {
            echo json_encode(array("insert_res"=>"110"));
        }
    }else{
        $result =   array("insert_res"=>"111");
        echo json_encode($result);
    }
}    
?>

您已对原始图像进行了编码,即base64_encode(),因此您需要反转此编码,使其再次成为图像。

test_disp.php

$conn  = mysqli_connect("localhost", "root", "root", "test");

if(isset($_GET['id'])){
    $id = mysqli_real_escape_string($conn, $_GET['id']);

    $sql       = "SELECT * FROM files WHERE id = '$id'";
    $result    = mysqli_query($conn, $sql);
    if ( $result === false ) {
        // check the query actually worked
        echo mysqli_error($conn);
        exit;
    }
    $row       = mysqli_fetch_assoc($result);

    header("content-type: image/jpg");
    echo base64_decode($row[data]);             // < added function

} else {
    echo 'error';
}

当查询只返回一个结果行时,您也不需要while循环!