2013-03-03 30 views
0

我使用的數據庫是Postgresql,我下載了一個網頁並存儲在一個BLOB中。像這樣:PHP將HTML編碼和解碼爲二進制

<?php 
$html = file_get_contents('http://www.example.com'); 
$encoded_html = base64_encode($html); 

//Store encoded data in blob in database 
?> 

這部分工作正常。但是,當我嘗試解碼並顯示它時,它會變得難以置信。

<?php echo base64_decode($encoded_html); ?> 

編碼和解碼數據時是否需要添加額外的參數?

+1

爲什麼要編碼?它需要33%的內存! – bitWorking 2013-03-03 22:41:52

+0

除此之外,如果你是base64編碼,那麼你不需要一個blob,只是純文本。 – 2013-03-03 22:43:36

+0

的確,照顧base64使用的地方。這是貪婪的記憶。你可以直接添加你的內容,轉義你的字符串以防止postGreSQL發生錯誤。這會很好,更簡單。 – MatRt 2013-03-03 22:43:45

回答

1

如果工作正常,則編碼/解碼功能,做工精細

<?php 
$html = file_get_contents('http://www.example.com'); 
$md5 = md5($html); 
$encoded_html = base64_encode($html); 
$decoded_html = base64_decode($encoded_html); 
echo (md5($decoded_html) == $md5) ? 'OK' : 'FAIL'; 
echo PHP_EOL; 

如果沒有,那麼我建議比較,你將進入數據庫,什麼出來以base64數據。

相關問題