2014-03-31 66 views
-1

我已成功將圖像存儲在mySql中。我想檢索出來並在網站上顯示一個可以放大它的href按鈕。檢索並顯示來自MySql的圖像

這是我的數據庫表結構

-- Table structure for table `poster` 
-- 

CREATE TABLE `image` (
    `img_id` int(11) NOT NULL auto_increment, 
    `img_name` varchar(32) NOT NULL, 
    `img` blob NOT NULL, 
    `img_type` varchar(32) NOT NULL, 
    `img_size` int(255) NOT NULL, 
    PRIMARY KEY (`img_id`) 
) 

地顯示圖像

<div class="col-md-5 text-dimension"> 
     <?php 
      $con=mysql_connect("localhost","root",""); 
      mysql_select_db("isiti"); 

      if (mysqli_connect_errno($con)) 
      { 
       echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
      } 

      $result = mysql_query("SELECT * FROM poster ORDER BY year DESC LIMIT 0, 5000"); 
       while($row = mysql_fetch_array($result)) 
       { 
        $id=$row['img_id']; 
        echo '<a href="img_disp.php?image_id='.$id.'" data-lightbox="image-1" title="©ISITI-CoERI"><center><img src="img_disp.php?image_id='.$id.'" alt="poster1" width="200" height="250"><br></a><br><br>'; 
        echo '<span class=right><a href="edit_form.php?id='.$id.'">[edit]</a> 
        <a name="delete" id="delete" href="public_delete.php?id='.$id.'">[delete]</a></span>'; 
       } 
     ?> 

這是img_disp.php

<?php 

    // Connect to the database 
     $host="localhost"; // Host name 
     $username="root"; // Mysql username 
     $password=""; // Mysql password 
     $db_name="isiti"; // Database name 
     $tbl_name="poster"; // Table name 

     $conn = mysql_connect("$host", "$username", "$password"); 
     if(! $conn) 
     { 
      die('Could not connect: ' . mysql_error()); 
     } 
     if (isset($_GET["image_id"]) && !empty($_GET["image_id"])) 
      {       
       $id =$_GET["image_id"]; 
      } 

     mysql_select_db($db_name); 

    $sql="SELECT * from poster where id='$id'"; 

    $query=mysql_query($sql) or die(mysql_error()); 

    while($result=mysql_fetch_array($query)){  
    header('Content-type:'.$result['img_type'].''); 
    echo $result['img']; 
    header('Content-Disposition: inline; filename="'.$result['img_name'].'"');  
    } 
?> 

問題是...圖像沒有顯示出來。爲兩者或彈出。

+0

你不能在回聲後再做header()。必須先發送所有標題,然後才發送數據。 – RobP

回答

0

我認爲問題在於你使用的是echo,它是爲了處理字符串。你也可以考慮其他的方式來存儲圖像,而不是使用mysql。無論如何,正確的做法是打開文件流,然後將數據放入其中。此外,您將需要一個單獨的文件來處理圖片,因爲您只能更改整個文件的標題。這意味着您需要一個文件來爲圖片創建一個顯示頁面,而另一個文件則需要提取圖片。

$write=fopen("php://output","w"); 
fwrite($write,$results['img']); 
fclose($write); 

另外我不確定你有mysql結果的結構。

0

我建議將所有圖像存儲在磁盤[文件系統]而不是MySQL。 您也可以加密圖像並將其存儲在.dat文件中。這將確保您的圖像的安全性。

在MySQL中只存儲該特定圖像的路徑。當你從MySQL中檢索記錄時

go to the image path 
get image 
decrypt it (Optional, Only if you have encrypted it before) 
and display the image.. 
+0

如何將圖像存儲到文件系統?有沒有例子? – ZKT

+0

如果您允許用戶上傳圖片,那麼您可以使用PHP move_uploaded_file將上傳的圖片/文件保存到您的文件夾中。 http://www.php.net/manual/en/function.move-uploaded-file.php – sravis

相關問題