开发者

Displaying BLOB files in PHP script

开发者 https://www.devze.com 2023-01-16 08:07 出处:网络
This is my PHP script which displays a radio station\'s schedule: <div class=\"divider\"></div>

This is my PHP script which displays a radio station's schedule:

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

The values with the dollar sign represent the field names in my database, which is radiopresenters.

How would I get this to work as a PHP script, and display the values from the database? All values in the fields are stored in TEXT format, apart from the image field which is stored in BLOB format.

Airtime is stored in a separate database entitled as radioschedule which has all 4 fields ib, and I intend to link these together via some relational means.

What's the best way to get it to display as the above, especially the BLOB part?


What you want to do is set up an show_image.php script, Example

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

Then within your html files you would do the follwing.

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

That's roughly how its done, another pointer is when your selecting your data via your main page, you should not select the blob data as it will slow your application down, and show_image.php may require more work as its for example purposes only

Peace.


You could write the image stored as a blob in the database out to a file. You could then use a url as your image source.

Assuming $presenter doesn't have any file/url reserved characters in it, and $image is stored as an accurate binary jpg (or png or gif etc.) , you could do:

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

I would suggest that you work out some way of caching the file, so it doesn't have to be written out for every page load though.


Are you asking how to put those values into your file?

As for the text ones you could just use

<?=$airtime?> 

or if your server setup doesn't support short tags just use

<?php echo $airtime?>

I would just do this inline with your php.

Example:

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

As for the BLOB, I would advise not doing this and just storing the image on your server and store the image file's name in the db. BUT if you are intent on doing this I think the only way you can do this is by having a separate script that returns an image file. Something like this:

<?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();
?>

Then in your other file you would do something like:

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

精彩评论

暂无评论...
验证码 换一张
取 消