【问题标题】:Displaying BLOB files in PHP script在 PHP 脚本中显示 BLOB 文件
【发布时间】:2010-09-15 06:18:57
【问题描述】:

这是我的 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 脚本工作,并显示数据库中的值? 字段中的所有值都以 TEXT 格式存储,除了 image 字段以 BLOB 格式存储。

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

什么是让它显示为上述的最佳方式,尤其是 BLOB 部分?

【问题讨论】:

    标签: php mysql blob


    【解决方案1】:

    您要做的是设置一个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 可能需要更多工作作为示例目的只有

    和平。

    【讨论】:

      【解决方案2】:

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

      假设 $presenter 中没有任何文件/url 保留字符,并且 $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">
      

      我建议你想出一些缓存文件的方法,这样就不必在每次页面加载时都写出来。

      【讨论】:

      • 每次运行的时候都不是完全缓存,还有就是图片文件存在,你的代码有点容易出错。
      • 确实——正是我建议在答案中添加缓存的原因。
      【解决方案3】:

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

      至于文字,你可以使用

      <?=$airtime?> 
      

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

      <?php echo $airtime?>
      

      我会在您的 php 中内联执行此操作。

      例子:

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

      至于 BLOB,我建议不要这样做,只需将图像存储在您的服务器上并将图像文件的名称存储在 db 中。但是,如果您打算这样做,我认为您可以做到这一点的唯一方法是拥有一个单独的脚本来返回图像文件。像这样的:

      <?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>
      

      【讨论】:

      • 我在询问如何创建一个将在包含文件中使用的脚本,以及如果在我的数据库中没有选择任何内容,则使用 ifelse。将这两个表链接在一起是一个问题 - 它会导致我在做关系数据库时遇到的由井号标记的关系错误吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 2017-07-28
      • 2011-08-28
      • 2023-03-23
      • 1970-01-01
      • 2018-12-07
      • 2017-10-18
      相关资源
      最近更新 更多