Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Blob Images with Functions

Posted by mr_yogi (mr_yogi), 23 October 2006
Hi,
I'm trying to keep my Web application down to 4 files (HTML Template, Main PHP, Web Helpers and Bus Logic) however I'm running into problems with images stored in MySQL Blobs.

I thought it would be neat to keep all my images (logos etc.) in MySQL blobs. I can add and select/ view the images by using separate programs. I have removed my application and created a test example:

test1.php
Code:
<?php
$imageID = "6";
?>
<html>
<head>
<title>Blob Test</title>
</head>
<body>
<table>
     <tr>
     <td>Porsche 944 Turbo S<br><font face="arial" size=2 color=blue>image stored in MySQL database.</font></td>
     </tr>
</table>
<img src="showImage.php?fID=<?php print $imageID; ?>" >
</body>
</html>


with showImage.php
Code:
<?php
$id_files=$_REQUEST['fID'];
if ($id_files) {
   include "Kristin/open_db_alpha.inc";
   $sql = "SELECT bin_data, filetype FROM tbl_Files WHERE id_files=$id_files";

   $result = @mysql_query($sql, $db);

   $data = @mysql_result($result, 0, "bin_data");
   $type = @mysql_result($result, 0, "filetype");

   header("Content-Type: $type");
   print $data;
}
?>

Works fine.

However when I try and make showImage a function rather than a separate file I get the binary string output in the HTML.
test2.php
Code:
<?php
$imageID = "6";

function showImage ($id_files) {
/* $id_files=$_REQUEST['fID'];*/
     if ($id_files) {
           include "Kristin/open_db_alpha.inc";
           $sql = "SELECT bin_data, filetype, filename, filesize FROM tbl_Files WHERE id_files=$id_files";

           $result = @mysql_query($sql, $db);
           $data = @mysql_result($result, 0, "bin_data");
           $type = @mysql_result($result, 0, "filetype");

           header("Content-Type: $type");
           print $data;

           }
     }

?>

<html>
<head>
<title>Table Test</title>
</head>
<body>
<table>
     <tr>
     <td>Porsche 944 Turbo S<br><font face="arial" size=2 color=blue>this image is stored in a MySQL database</font></td>
     </tr>
</table>
<?php showImage($imageID); ?>
<!-- <img src="<?php showImage($imageID); ?>" > -->
<!-- <img src="showImage.php?fID=<?php print $imageID; ?>" > -->
</body>
</html>



Any ideas what I'm doing wrong? I haven't been able to find a great deal online about this stuff, mostly people creating thumnails. I have tried various ways to call the function (some are rem'd out in the above code).


Thanks

Kristin

Posted by admin (Graham Ellis), 23 October 2006
With a function call, you're trying to include the binary stream for an image within the HTML, and that's not how it works, I'm afraid    . You will need to keep the Image-from-MySQL code in a separate file, with its own URL, exactly as you did in  your "works fine" case.

Now ... I HAVE written a piece of code that included both the image generation and the HTML in the same page and it sent out either an image/jpeg or a text/html depending on what paramaters were passed in.   But I do NOT recommend you do so; in hindsight, it was a very poor design - I think they call it "job protection code"!



Posted by mr_yogi (mr_yogi), 23 October 2006
Thanks  



This page is a thread posted to the opentalk forum at www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho