Blob image not displaying in php

so i'm having a small problem in displaying a BLOB from my database , I tried everything but with no results , just some ugly characters , I know it's something silly but it drives me crazy , here is the code:

 
'.$title.'
'.$keywords.'
'.$description.'
'; } }

} ?>

In my database picture is set to 'blob' , the rest is simple , I also have a print("$output") on my page but that has nothing to do with this , I hope there could be someone free to help :D

Create a new PHP file ac_logo_upload.php. The initial contents are:

getSession();
if (!isset($sess->username) || empty($sess->username)
        || !$sess->isPrivilegedUser()) {
    header('Location: index.php');
    exit;
}
 
$page = new \Equipment\Page;
$page->printHeader("AnyCo Corp. Upload Logo");
$page->printMenu($sess->username, $sess->isPrivilegedUser());
printcontent($sess);
$page->printFooter();
 
// Functions
 
?>

Add the printcontent() function:

/**
 * Print the main body of the page
 *
 * @param Session $sess
 */
function printcontent($sess) {
    echo "
"; if (!isset($_FILES['lob_upload'])) { printform(); } else { $blobdata = file_get_contents($_FILES['lob_upload']['tmp_name']); if (!$blobdata) { // N.b. this test could be enhanced to confirm the image is a JPEG printform(); } else { $db = new \Oracle\Db("Equipment", $sess->username); $sql = 'INSERT INTO pictures (pic) VALUES(EMPTY_BLOB()) RETURNING pic INTO :blobbind'; $db->insertBlob($sql, 'Insert Logo BLOB', 'blobbind', $blobdata); echo '

New logo was uploaded

'; } } echo "
"; // content }

This is in the now familiar two part structure with an HTML form and a form-handler. The INSERT statement uses a bind value to represent the BLOB. The new Db class insertBlob() will associate the BLOB data with the bind variable and commit the record. The uploaded image will be added to the PICTURES table.

Complete ac_logo_upload.php by adding the form function printform():

/**
 * Print the HTML form to upload the image
 *
 * Adding CSRF protection is an exercise for the reader
 */
function printform() {
    echo <<
Image file name:

Note:

The EOF; token must be at the start of a line and not have trailing white space.

When this form is submitted the PHP web server will be able to access uploaded BLOB data in the temporary file $_FILES['lob_upload']['tmp_name'], as seen in printcontent().

PHP has various options controlling locations and upper sizes of files, refer to the PHP documentation. The AnyCo application will use the default values.

Edit ac_db.inc.php and add the insertBlob() method to the Db class:

    /**
     * Insert a BLOB
     *
     * $sql = 'INSERT INTO BTAB (BLOBID, BLOBDATA)
     *        VALUES(:MYBLOBID, EMPTY_BLOB()) RETURNING BLOBDATA
     *        INTO :BLOBDATA';
     * Db::insertblob($sql, 'do insert for X', myblobid', 
     * $blobdata, array(array(":p", $p, -1)));
     *
     * $sql = 'UPDATE MYBTAB SET blobdata = EMPTY_BLOB()
     *        RETURNING blobdata INTO :blobdata';
     * Db::insertblob($sql, 'do insert for X', 'blobdata', $blobdata);
     *
     * @param string $sql An INSERT or UPDATE statement that
     * @returns a LOB locator
     * @param string $action Action text for End-to-End Application Tracing
     * @param string $blobbindname Bind variable name of the BLOB
     * @in the statement
     * @param string $blob BLOB data to be inserted
     * @param array $otherbindvars Bind variables. An array of tuples
     */
    public function insertBlob($sql, $action, $blobbindname, $blob, 
    $otherbindvars = array()) {
        $this->stid = oci_parse($this->conn, $sql);
        $dlob = oci_new_descriptor($this->conn, OCI_D_LOB);
        oci_bind_by_name($this->stid, $blobbindname, $dlob, -1, OCI_B_BLOB);
        foreach ($otherbindvars as $bv) {
            // oci_bind_by_name(resource, bv_name, php_variable, length)
            oci_bind_by_name($this->stid, $bv[0], $bv[1], $bv[2]);
        }
        oci_set_action($this->conn, $action);
        oci_execute($this->stid, OCI_NO_AUTO_COMMIT);
        if ($dlob->save($blob)) {
            oci_commit($this->conn);
        }
    }

The insertBlob() method accepts a final option parameter for normal bind variables. This is not used when it is called in printcontent() in ac_logo_upload.php.

The BLOB is bound as a special type, similar to how a REF CURSOR was bound in the “Showing Equipment Records by Using a REF_CURSOR”. PHP OCI8 also has a OCI_B_CLOB constant that can be used for binding CLOBs. The LOB descriptor is an instance of PHP OCI8's OCI-Lob class, which has various methods for uploading and reading data. When oci_execute() is processed on the SQL INSERT statement the OCI_NO_AUTO_COMMIT flag is used. This is because the database transaction must remain open until the $dlob->save() method inserts the data. Finally, an explicit oci_commit() commits the BLOB.

Run the AnyCo application in a browser and log in Administrator. Click the Upload Logo link in the left hand menu. Locate a JPEG image on your computer and select it. The next section of this chapter will display the image in the page header with the title, so choose an image of 15 to 20 pixels in height.

Blob image not displaying in php

Click the Upload button.