如何将图像存储在mysql表中并使用php代码检索存储的图像?

问题描述:

我需要帮助的是如何将图像存储在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

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