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
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
PHP - processing images as mySQL Blobs

Posted by sisko (sisko), 4 October 2007
Firstly, I upload images via a simple form uploading facility. The image's byte data is then saved to mysql in a BLOB field.

Secondly, I try reading that information back and reconstituing the image for displaying on my webpage(s).

I need help with this process as I don't know where I am going wrong.

This is the function used to load the blob into mysql :
[PHP]
function test(){
         
           if( move_uploaded_file($_FILES['image']['tmp_name'],"management/test/".$_FILES['image']['name']) ){
               echo "<br> File move sucessful!";
               echo "<br> File Size : ".filesize("management/test/".$_FILES['image']['name'])."<br>";
           }else{
               echo "<br> File move Failed!";
           }
         
           $instr = fopen( "management/test/".$_FILES['image']['name'],"rb" );
           $data = addslashes(fread($instr,filesize("management/test/".$_FILES['image']['name'])));

           echo "<br> ".$data;
         
           if( !(mysql_query("insert into gallery (title, ext, data) values ('test', 'jpeg', \"".$data."\")") ) )
           {
               echo "<br> We Have A Problem!";
           }
}
[/PHP]

And this is the code to display the image :
[PHP]
function test2(){
           $rs = $this->adodb->execute("SELECT * FROM gallery");
           while( $obj = $rs->FetchRow() )
           {
               /*echo "<br> title : ".$obj['title'];
               echo "<br> ext : ".$obj['ext'];
               echo "<br> time : ".$obj['image_time'];
               echo "<br> String lenght : ".strlen($obj['data']);
               echo "<br> data : ".$obj['data'];
               echo "<br> ************************************* ";*/
             
               // outputing HTTP headers
//                header('Content-Length: '.strlen($obj['data']));
               header("Content-type: image/".$obj['ext']);
         
               // outputing image
//                echo "<br> checking image output : ".$obj['data'];
               print( $obj['data'] );
           }
       }
[/PHP]

When I try executing test2(), All I get is the first couple of byte code characters and nothing else.


Posted by admin (Graham Ellis), 4 October 2007
Are you doing ANYTHING else except sending the image out from yor web page that calls function test2?   In particular, are you sending out some other content?

Posted by sisko (sisko), 4 October 2007
Hi Graham,

the answer is no. I'm not sending out any other content. The output simply break a few characters into the byte data.

So, image seeing "&#65533;&#65533;&#65533;JFIF" (quotes not included) and nothingelse as the output to image byte code for an image of 2215 bytes.

If I'm doing addslashes on my image data and apache has magic_quotes_gpc set to On, does that represent a problem?
I tried setting it to off but it was all the same

Posted by sisko (sisko), 4 October 2007
I just thought I'd mention aswell, the webpage is not all clear and exclusively attempting to show the image.

It delivers other content not unlike your example webpage

Posted by admin (Graham Ellis), 5 October 2007
It looks to me like you might be sending something ahead of the header. I suspect the bits outside the function you have shown us. Have you tried calling up the page with telnet rather than with a browser to see exactly what is generated?  

Posted by sisko (sisko), 6 October 2007
on 10/05/07 at 23:03:55, Graham Ellis wrote:
It looks to me like you might be sending something ahead of the header. I suspect the bits outside the function you have shown us. Have you tried calling up the page with telnet rather than with a browser to see exactly what is generated?  



Forgive my ignorance but I have no idea how to test the page with telnet.

Also, I thought the issue was that the byte date was somehow corrupted? Either in writing it in or reading it out.


Posted by admin (Graham Ellis), 6 October 2007
I suspect you may have extra bytes / warning messages coming out of the script - from the bit that's outside the function - which is why I suggested that you (a) show that code and (b) test it away from a browser.


Telnet browsing - please excuse the highlighting of the URLs

telnet www.somehost.com 80
GET /filename.html HTTP/1.1
host: www.somehost.com
<a blank line> .....




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., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho