Jump to content

PHP - processing images as mySQL Blobs


sazzie

Recommended Posts

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 :

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!";
            }
}

 

And this is the code to display the image :

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'] );
            }
        }

 

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.