2010-09-20 88 views
0

我需要幫助的是如何將圖像存儲在MySQL表,並使用PHP代碼存儲的圖像檢索...如何將圖像存儲在mysql表中並使用php代碼檢索存儲的圖像?

示例代碼,可以幫助我很多..

問候 〜迪普〜

+2

缺乏細節,是http://www.phpriot.com/articles/images-in-mysql你是什麼意思? – Gazler 2010-09-20 17:59:53

+0

我不建議將它們存儲在數據庫中。我建議將它們存儲在文件系統中,並在數據庫中存儲圖像的路徑。 – Bot 2010-09-20 19:40:00

回答

1

Blob字段和另一個用於圖片類型(jpeg/gif/etc ..)的varchar字段在數據庫中創建表,將圖片存儲在那裏。

存儲圖片做到以下幾點:

  1. 閱讀圖片插入變量。無論是到數據庫

file_get_contentsfread

  • 插入圖片數據和圖像類型檢索圖片回做定期的select語句來獲取圖像數據和文件類型。 將標頭Content-type設置爲適當的文件類型並顯示圖片數據。

    例如:

    HTML
    <img src="getPicture.php?id=12345" />

    PHP

    <?php 
    $id = (int) $_GET['id']; 
    // Assume $db is out DAL that is already connected and can query database 
    $img = $db->loadObject("SELECT pic_data, pic_type FROM picture WHERE id = $id LIMIT 1"); 
    
    // We get the following 
    // $img->pic_type = 'image/jpeg' 
    // $img->pic_data = 'picture data' 
    
    // 
    // 
    // Make sure there is not output prior setting header 
    header("Content-type: $img->pic_type"); 
    echo $img->pic_data; 
    

    看這個頁面。有代碼會幫你http://www.anyexample.com/programming/php/php_mysql_example__image_gallery_(blob_storage).xml

  • +0

    +1很好的解釋 – 2010-09-20 20:16:52