dsl2014 2016-11-16 04:08
浏览 48

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

I am facing an issue to get images from database. I went through so many articles of stack and other websites also, but none have the solution I am looking for. How can I get images stored in the database on the page?

Here is the code, kindly check it and let me know what mistake I made. All help is really appreciated.

<?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条回答 默认 最新

  • duanfan5012 2016-11-16 05:03
    关注

    Step 1: create a php script with filename getimage.php with the following code:

    <?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'];
    ?>
    

    Step 2: In your current php page (below the comment "# below is the code i tried") try the following code:

    $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" />';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大