2014-11-20 88 views
2

我閱讀了所有關於StackOverflow的可能的問題,我問了所有我知道但沒人能幫助我的人。 我有表數據庫:PHP顯示圖像爲BLOB mysqli

CREATE TABLE IF NOT EXISTS `zdjecia` (
`id` int(11) NOT NULL, 
    `imie_wlasciciela` varchar(50) NOT NULL, 
    `zdjecie` blob NOT NULL, 
    `nazwa` varchar(50) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=16 

我爲不懂英語的變量抱歉,但我希望將所有源沒有改變。 imie_wlascicielanazwa在我的問題中並不重要。 所以zdjecie是圖像的二進制數據 而id是id。

我有一個文件,應該顯示圖像。

// single_image.php

<?php 

if (isset($_GET['id'])) 
{ 
    include('../connect.php'); // connect to database 
    $id = $_GET['id']; 
    $ret = $conn->query("SELECT * FROM zdjecia WHERE id=$id"); 
    $row = $ret->fetch_assoc(); 
    header("Content-type: image/png"); 
    echo $row['zdjecie']; 
    // echo $row['nazwa']; // correct the result. 
} 
?> 

在我的瀏覽器URL i貼.../single_image.php?ID = 8

我的問題是不顯示圖像,我在數據庫幾張圖片。我可以通過/ phpmyadmin在XAMPP下載它,它是正確的圖像,但我的頁面無法正確顯示, 每次看到一個破損的圖像圖標。

文件single_image.php設置爲無彈和Code-Page 1250(中歐)。

當我檢查結果$row['nazwa'],並評論header("Content-type...")行它顯示正確的varchar(50)從數據庫。

我使用它就像寫了幾行字以上,或HTML,<img src="single_image.php?id=8"/>

+0

工作,如果你正在使用urlrewrite有時你必須添加斜槓「/」到你的圖像源的HTML端。像 hakiko 2014-11-20 16:56:02

+0

i dont understand your last sentence. there is no source code in page when there is only an image. – Jan3Sobieski 2014-11-20 17:01:30

+0

If you change your file's extention to png you must change its image header info. I assumed, you only changed file name from xx.jpg to xx.png Sometimes this works but not completely. Still your file has .jpg info. So please give some more details about your images – hakiko 2014-11-20 17:06:04

回答

2

試試下面

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

爲了更好的維護您可以添加data:image/png;base64爲你的頭那麼只有echo base64_encode($row['zdjecie'])這會爲single_image.php?id=$id

+0

it works. image is showed, but i dont know... i wanted to use my file single_image.php as a filepath, so i could in another files write 和操縱高度和寬度和樣式,其中需要, 你張貼工作代碼,正確。但很難維持。 – Jan3Sobieski 2014-11-20 17:19:06

+0

我付出了努力就可以解決我發佈的想法。 如果我錯了,告訴我。也許整個問題都不應該出現。 – Jan3Sobieski 2014-11-20 17:20:43

+0

我該怎麼辦? header(「Content-type:image/png」);這個東西改成 header(「Content-data:image/png; base64」); ? – Jan3Sobieski 2014-11-20 17:33:20

0
  1. 圖像不存儲在數據庫,它的性能自殺。
  2. 不要讓您的代碼容易受到SQL注入攻擊。
  3. 如果您必須將文件存儲在數據庫中,請存儲內容類型。也許這不是一個PNG?或者,您可能會打印出警告/通知?它會打破你的形象。
+0

This is not an answer to this question – hakiko 2014-11-20 16:59:15

+0

Look at last point ;] – 2014-11-20 16:59:56

+0

I have no errors showing by php interpreter or browsware console. i "saved as" few data from database to my documents, change extension to .png and it works fine. i really want to do that with blobl. i know it isnt a good idea. and i know how to defend agains sql injection, but to clear code i remove all. – Jan3Sobieski 2014-11-20 17:00:17