2012-05-08 24 views
0

我想從網站上閱讀.ico-Images,將信息存儲到數據庫中,稍後在全球網站上顯示圖像。PHP Ico-Image存儲在數據庫中,未在瀏覽器中顯示

我已經設法將一個字符串中的圖像保存到數據庫中。在網站上顯示圖像的步驟是我的問題。

對於閱讀內容:

$data=file_get_contents("http://www.google.com/favicon.ico"); 
$data = base64_encode($data); 

什麼是顯示在網站上的單個的div圖像的正確方法?

Fesp

+1

唐不保存數據庫中的文件。將它們保存在文件系統中,並將其路徑存儲在數據庫中。 –

+0

看看這篇維基百科文章。 http://en.wikipedia.org/wiki/Data_URI_scheme您可以使用編碼的字符串作爲圖像源 – sofl

回答

0

您基本上需要告訴您的腳本輸出內容作爲ico類型。

<?php 
//Getting your image 
$data=file_get_contents("http://www.google.com/favicon.ico"); 
$data = base64_encode($data); 

//If your storing in the db then you do the query ect togo get the data string 

//Then echo out like this 
header('Content-Type: image/vnd.microsoft.icon'); 
echo base64_decode($data); 
?> 

記住你不能輸出任何設置標題之前,所以也許你會需要一個單獨的腳本來獲取文件和輸出中如此

<img src="get_ico.php?id=1"/>

然後在get_ico.php

<?php 
//Connect db ect 

//Query db for $_GET['id'] 

//Then echo out like this 
header('Content-Type: image/vnd.microsoft.icon'); 
echo base64_decode($row['image_data']); 
?> 
相關問題