在PHP脚本中显示BLOB文件

时间:2010-09-14 22:02:53

标签: php mysql blob

这是我的PHP脚本,显示了广播电台的时间表:

    <div class="divider"></div>
    <div class="main" style="width:552px;">
        <img src="$image" width=115 height=60>
        <div class="time">$airtime</div>
        <div class="show"><h3><a href="$link><b>$presenter</b></a></h3>
            <p>$showdesc</p></div>
        <div class="footer"></div>
    </div>
    </div>
                <div class="footer"></div>
                <div class="bottom"></div>
            </div>

带有美元符号的值表示我的数据库中的字段名称,即radiopresenters。

我如何将其作为PHP脚本工作,并显示数据库中的值? 除了以BLOB格式存储的图像字段外,字段中的所有值都以TEXT格式存储。

Airtime存储在一个名为 radioschedule 的单独数据库中,该数据库包含所有4个字段ib,我打算通过一些关系方式将它们链接在一起。

最好的方法是将它显示为如上所示,尤其是BLOB部分?

3 个答案:

答案 0 :(得分:4)

您要做的是设置show_image.php脚本,示例

show_image.php

<?php
$id = (isset($_GET['blid']) && is_numeric($_GET['blid'])) (int)$_GET['blid'] : false;

if($id)
{
    if(false !==($res = mysql_query('SELECT blob_data FROM table WHERE blob_id = ' . $id))
    {
        $data = mysql_fetch_assoc($res);
        header("Content-type: image/jpg"); //Send the content Type here.
        print $data['blob_data'];
        exit;
    }
}
?>

然后在您的html文件中,您将执行以下操作。

<div class="divider"></div>
<div class="main" style="width:552px;">

    <img src="show_image.php?id=<?php echo (int)$id?>" width=115 height=60>

    <div class="time">$airtime</div>
    <div class="show">
        <h3><a href="$link><b>$presenter</b></a></h3>
        <p>$showdesc</p></div>
        <div class="footer"></div>
    </div>
</div>
<div class="footer"></div>
<div class="bottom"></div>

这大致是它的完成方式,另一个指针是当你通过主页面选择数据时,你不应该选择blob数据,因为它会减慢你的应用程序速度,而show_image.php可能需要更多工作,例如用途仅

和平。

答案 1 :(得分:2)

您可以将存储为数据库中的blob的图像写入文件。然后,您可以使用网址作为图片来源。

假设$presenter中没有任何文件/网址保留字符,$image存储为准确的二进制jpg(或png或gif等),您可以这样做:

<?php
    if($fh = fopen("/webroot/images/{$presenter}.jpg", "wb")) {
        fwrite($fh, $image) ;
        fclose($fh) ;
    }
?>
<img src="/images/<? echo $presenter ; ?>.jpg" width="115" height="60">

我建议你找出一些缓存文件的方法,所以不必为每个页面加载都写出来。

答案 2 :(得分:0)

您是否在询问如何将这些值放入您的文件中?

至于文本,你可以使用

<?=$airtime?> 

或者如果您的服务器设置不支持短标签,请使用

<?php echo $airtime?>

我会用你的php内联。

示例:

<div class="time"><?=$airtime?></div>

对于BLOB,我建议不要这样做,只是将图像存储在服务器上并将图像文件的名称存储在数据库中。但是如果你打算这样做我认为你能做到这一点的唯一方法就是有一个单独的脚本来返回一个图像文件。像这样:

<?php 
// Do all your db stuff here, grab the blob file. Probably from a $_GET paramater
$image = $row['image'];
header("Content-type: image/jpeg"); // or gif, etc...
echo $image;
die();
?>

然后在你的另一个文件中你会做类似的事情:

<img src="imagescript.php?person_id=<?=$person_id?>" width=115 height=60>