如何使用pdo或mysqli在同一页面上从数据库(blob列)获取图像? PHP

时间:2016-11-16 04:08:39

标签: php mysqli pdo

我面临从数据库获取图像的问题。我也经历了很多堆栈和其他网站的文章,但没有一个有我想要的解决方案。如何在页面上获取存储在数据库中的图像?

这是代码,请仔细检查并让我知道我犯了什么错误。非常感谢所有帮助。

<?php
    // I have database name databaseimage
    // I have a table named store
    // I have 3 columns into the table store
    // id (int primary key ai), name (varchar), and image (longblob)

// prevent accesing the page directly
if($_SERVER['REQUEST_METHOD'] !='POST') {
    echo "you can not acces this page directly";
    die();
} 
else {

print_r($_FILES);


// file properties

$file = $_FILES['image']['tmp_name'];
echo "<br>$file";

if(!isset($file)){
    echo "please select an image";
    die();
} else {
//actual image
$image = addslashes(file_get_contents($_FILES['image']['tmp_name'])) ;

 // image name
    $image_name = addslashes($_FILES['image']['name']);
    echo "<br> image name is = $image_name";

//geting image size to check whether it is actually an image or not
$image_size = getimagesize($_FILES['image']['tmp_name']); 
echo "<br>";
print_r($image_size);

    if($image_size==FALSE) {
        echo "plese select an images only";
            die();
        }
    else {

        #code to put an image into the database

                // connect to database

try {
    $con = new PDO("mysql:host=localhost;dbname=databaseimage", "root",
     "");
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    echo "<br>connection succesfull";

    // make a query
    $extr = $con->prepare("INSERT INTO store (name, image) 
                                    VALUES (:na , :im)" );

    //$a=1;
    //$extr->bindParam(':i', $a);
    $extr->bindParam(':na', $image_name);
    $extr->bindParam(':im', $image, PDO::PARAM_LOB);

    if ( $extr->execute()==true) {
        echo "<br>image uploaded succesfully";

 # insert is working. I can insert images into database

 # but really facing problem while displaying those images
 # below is the code I tried

 # CODE for to show uploaded files
 #  to show images which is uploaded
        $show = $con->prepare("SELECT * FROM store ");
        //$a = 1;
        //$show->bindParam(':iam', $a);
        $show->execute();



        while ($row = $show->fetch(PDO::FETCH_BOUND) ) {
            # SHOW IMAGES 

             //echo "<img src = '$row[image]' alt='image'  >";
echo '<img src= "data:image/png;base64,'.base64_encode($row['image']).'" 
                                    height="100" width="100" />';
        }


} else {
    echo "<br>image not uploaded";
    }

}
catch(Exception $e)
    {
    echo "<br> Connection failed: " . $e->getMessage();
    }
        }
}

// disconnect the connection
$con = null;
}
?>

1 个答案:

答案 0 :(得分:-1)

步骤1:使用文件名getimage.php创建一个php脚本     以下代码:

<?php
$id = $_GET['id'];
// do some validation here to ensure id is safe

$con = new PDO("mysql:host=localhost;dbname=databaseimage", "root",
 "");  
$sql = "SELECT image FROM store WHERE id=$id";
$stmt=$con->query($sql);
$res=$stmt->fetch(PDO::FETCH_ASSOC);
$con=null;    

header("Content-type: image/pn")g;
echo $res['image'];
?>

步骤2:在您当前的php页面中(在评论&#34下面;#下面是我试过的代码&#34;)尝试以下代码:

$show = $con->prepare("SELECT id FROM store ");
//$a = 1;
//$show->bindParam(':iam', $a);
$show->execute();

while ($row = $show->fetch(PDO::FETCH_NUM) ) {
    # SHOW IMAGES              
    echo '<img src="getimage.php?id="'.$row[0].'" 
                                height="100" width="100" />';
}