2017-06-05 150 views
0

我正在使用PDO和PHP。使用PDO插入并从PostgreSQL bytea中检索图像

这是我在Postgre

CREATE TABLE public.img 
(
    id integer NOT NULL DEFAULT nextval('img_id_seq'::regclass), 
    nombre bytea 
) 

表当我保存我用这个方法将文件数据

<?php 
$db = new PDO("pgsql:host=localhost;port=5432;dbname=sac;user=postgres;password=insertPass"); 
if(isset($_POST["insert"])) 
{ 
     $file = pg_escape_bytea(addslashes(file_get_contents($_FILES["image"]["tmp_name"]))); 
     $sql = 'INSERT INTO img (nombre) VALUES (?)'; 
     $ISp_Res = $db->prepare($sql); 
     $ISp_Res->bindParam(1, $file); 
     $ISp_Res->execute(); 
} 
?> 

并在表中的值是

ID nombre 
4; "\377\330\377\340\\0\020JFIF\\0\001\001\\0\\0\001\\0\001\\0\\0\377\341\\0\234Exif\\0\\0II*\\0\010\\0\\0\\0\007\\0\\0\001\003\\0\001\\0\\0\\0S\002\\0\\0\001\001\003\\0\001\\0\\0\\0\026\002\\0\\0\022\001\003\\0\001\\0\\0\\0\\0\\0\\0\\02\001\002\\0\024\\0\\0\\ (...)" 

而形式,我检索我的表中的值

<?php 
       $query = "SELECT * FROM img ORDER BY id DESC"; 
       $db = new PDO("pgsql:host=localhost;port=5432;dbname=sac;user=postgres;password=insertPass"); 
       $result = $db->prepare($query); 
       $results = $result->execute(); 
       $results = $result->fetchAll(PDO::FETCH_ASSOC); 
       foreach($results as $row) { 
       echo($row['nombre']); 
        $dat= pg_unescape_bytea($row['nombre']); 
        echo "<img src='".$dat."'"; 
       } 
?> 

但是当我尝试获取信息,我只是得到资源ID#2警告:pg_unescape_bytea()预计参数1是字符串

这是testView

<script> 
 
$(document).ready(function(){ 
 
     $('#insert').click(function(){ 
 
      var image_name = $('#image').val(); 
 
      if(image_name == '') 
 
      { 
 
       alert("Please Select Image"); 
 
       return false; 
 
      } 
 
      else 
 
      { 
 
       var extension = $('#image').val().split('.').pop().toLowerCase(); 
 
       if(jQuery.inArray(extension, ['gif','png','jpg','jpeg']) == -1) 
 
       { 
 
        alert('Invalid Image File'); 
 
        $('#image').val(''); 
 
        return false; 
 
       } 
 
      } 
 
     }); 
 
}); 
 
</script>
<?php 
 
$db = new PDO("pgsql:host=localhost;port=5432;dbname=sac;user=postgres;password=insertPass"); 
 
if(isset($_POST["insert"])) 
 
{ 
 
     $file = pg_escape_bytea(addslashes(file_get_contents($_FILES["image"]["tmp_name"]))); 
 
     $sql = 'INSERT INTO img (nombre) VALUES (?)'; 
 
    \t \t $ISp_Res = $db->prepare($sql); 
 
    \t \t $ISp_Res->bindParam(1, $file); 
 
    \t \t $ISp_Res->execute(); 
 
} 
 
?> 
 
<!DOCTYPE html> 
 
<html> 
 
     <head> 
 
      <title>Webslesson Tutorial | Insert and Display Images From Mysql Database in PHP</title> 
 
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script> 
 
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" /> 
 
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 
 
     </head> 
 
     <body> 
 
      <br /><br /> 
 
      <div class="container" style="width:500px;"> 
 
       <h3 align="center">Insert and Display Images From Mysql Database in PHP</h3> 
 
       <br /> 
 
       <form method="post" enctype="multipart/form-data"> 
 
        <input type="file" name="image" id="image" /> 
 
        <br /> 
 
        <input type="submit" name="insert" id="insert" value="Insert" class="btn btn-info" /> 
 
       </form> 
 
       <br /> 
 
       <br /> 
 
       <table class="table table-bordered"> 
 
        <tr> 
 
          <th>Image</th> 
 
        </tr> 
 
       <?php 
 
       $query = "SELECT * FROM img ORDER BY id DESC"; 
 
       $db = new PDO("pgsql:host=localhost;port=5432;dbname=sac;user=postgres;password=insertPass"); 
 
       $result = $db->prepare($query); 
 
       $results = $result->execute(); 
 
       $results = $result->fetchAll(PDO::FETCH_ASSOC); 
 
       foreach($results as $row) { 
 
       echo($row['nombre']); 
 
        $dat= pg_unescape_bytea($row['nombre']); 
 
        echo "<img src='".$dat."'"; 
 
       } 
 
       ?> 
 
       </table> 
 
      </div> 
 
     </body> 
 
</html>

请告诉我在哪里做错了:(

回答

0

插入时不要使用addslashes()。

$target_file = '/path/to/file.jpg'; 
$img = fopen($target_file, 'r'); 
$data = fread($img, filesize($target_file)); 
$file = pg_escape_bytea($data); 

在拿数据使用

ob_start(); 
fpassthru($row['nombre']); 
$dat= ob_get_contents(); 
ob_end_clean(); 
$dat= "data:image/*;base64," . base64_encode($dat); 
echo "<img src='".$dat."'";