#native_company# #native_desc#
#native_cta#

uploading/downloading images in oracle using PHP

By Radha Krishna G
on July 20, 2001

Version: 1.0

Type: Sample Code (HOWTO)

Category: Databases

License: GNU General Public License

Description: This snippet will help in understanding the uploading/down loading the images into databases. You can upload any file.But it is limited to .gif files only when down loading. You can make chages to the code so that it will work for diffrent image types.

uploadimage.php:


<HTML>
<HEAD><TITLE>Store binary data into SQL Database</TITLE></HEAD>
<BODY>
<?php
        if ($submit) {
                $data = (fread(fopen($form_data, "r"), filesize($form_data)));
						//You have to pass Appropriate username, Password,Serveice name
                        $db = OCILogon("scott","tiger","grk");
                        $stmt = OCIParse($db,"insert into blob_table values ($id,EMPTY_BLOB()) returning id,IMG into :id,:lob");
                        $lob = OCINewDescriptor($db);
                        OCIBindByName($stmt,":ID",$id,32);
                        OCIBindByName($stmt,":LOB",$lob,-1,SQLT_BLOB);
						// we cannot use autocommitt here
                        $exec_result=OCIExecute($stmt,OCI_DEFAULT);
                        $lob->save($data);
                        echo "$form_data id:$idn";
                        OCICommit($db);
                        if(!$exec_result){
                                        print "Error Occured".OCIError($conn);
                        }else{
                                        OCICommit ($db);
                                        print "<p>This file has been uploaded into Database ";
                        }
        } else {
?>
    <form method="post" action="uploadimage.php" enctype="multipart/form-data">
    File Description:<br>
    <input type="text" name="id"  size="40">
    <INPUT TYPE="hidden" name="MAX_FILE_SIZE" value="1000000">
    <br>File to upload/store in database:<br>
    <input type="file" name="form_data"  size="40">
    <p><input type="submit" name="submit" value="submit">
    </form>
<?php
}
?>
</BODY>
</HTML>

display.php:

<html><body>
<?
//You have to pass Appropriate username, Password,Serveice name
$db = OCILogon("scott","tiger","grk");
$stmt = OCIParse($db,"select * from blob_table order by id");
OCIExecute($stmt);
$img='';
$count=0;
while (OCIFetchInto($stmt,$arr,OCI_ASSOC)) {
        $count++;
    echo "id: ".$arr[ "ID" ]."n";
    $img= ($arr[ "IMG" ]->load());
        if($fp = fopen ("test".$count.".gif", "w")){
                fwrite ($fp, $img);
                echo"&nbsp;<img src="test".$count.".gif"><br><hr>";
        }else{
                echo ("failed to open the file");
        }
}
?>
</body></html>