Page 1 of 1

Load & display image in postgresql

Posted: Tue Nov 02, 2010 2:31 am
by carinp
Please help! I can't get this to work! I need to load an image to a blob field in postgresql and display it again.

My upload code - that seems to be working:

Code: Select all

            $vFileName = $_FILES["image"]["name"];  //The original name of the file on the client machine. 
            $vTmpName = $_FILES["image"]["tmp_name"];  //The temporary filename of the file in which the uploaded file was stored on the server.
            $vFileSize = $_FILES["image"]["size"];  //The size, in bytes, of the uploaded file. 
            $vFileType = $_FILES["image"]["type"];  //The mime type of the file

            $vFp = fopen($vTmpName, "r");
            $vContent = fread($vFp, filesize($vTmpName));
            $vContent = pg_escape_bytea($vContent);
            $vFileName = pg_escape_bytea($vFileName);
            fclose($vFp);        
      
            if(!get_magic_quotes_gpc()){
              $vFileName = addslashes($vFileName);
            }                     
            
            $sqlStringBlob = "INSERT INTO qa_blobs (blob_file_name, blob_content_type, blob_size, blob_object) VALUES ('$vFileName', '$vFileType', '$vFileSize', '$vContent')";
            pg_query($sqlStringBlob);
Here is my display code - that displays no image (red X - image not found)

Code: Select all

    $sqlString = "SELECT blob_file_name, blob_content_type, blob_size, blob_object from qa.qa_blobs where blob_id = ".$_GET['id'];     	
    $result=pg_query($conn, $sqlString);

    while ($a_row = pg_fetch_array($result)) {  
        $blob_file_mimetype = $a_row['blob_content_type'];
        $blob_file = pg_unescape_bytea($a_row['blob_object']);
    }
    pg_free_result($result); 

header("Content-Type:  $blob_file_mimetype ");
echo $blob_file;
What is wrong?

Re: Load & display image in postgresql

Posted: Tue Nov 02, 2010 5:21 am
by cpetercarter
Why are you trying to store an image in a database?

The normal approach is to store the image in a directory, and use the database to keep details of the image (its name, size, location etc).