//store.php
<html>
<head><title>store binary data into oracle database</title></head>
<body>
<?php
// 如果提交了表单,代码将被执行:
dl("php_oci8.dll");
$conn = ocilogon("scott","tiger");
if ($submit) {
echo "file name: <b>$userfile_name</b><br>\n";
echo "file size: <b>$userfile_size</b><br>\n";
$hwsize = getimagesize($userfile );
$w = $hwsize[0];
$h = $hwsize[1];
echo "image width: <b>$w</b><br>\n";
echo "image height: <b>$h</b><br>\n";
$imgtype = strtolower(substr( strrchr( $userfile_name, "." ), 1 ) );
if ($imgtype == "jpg")
$imgtype = "jpeg";
echo "image type: <b>$imgtype</b><br>\n";
echo "created date; <b>".date(y-m-d)."</b><br>\n";
$sql = "insert into
picture (picid, username, width, height, imgsize, imgtype, created, image, filename, description)
values(picturepicid.nextval, $username, $w, $h, $userfile_size, $imgtype, to_date(".date(y-m-d).",yyyy-mm-dd), empty_blob(), $userfile_name, $description)
returning image into :image";
echo "<pre>$sql</pre>";
$stmt = ociparse($conn, $sql );
$image = ocinewdescriptor($conn );
ocibindbyname($stmt, ":image", $image, -1, sqlt_blob );
if (!ociexecute($stmt, oci_default)) {
echo "execution failed";
exit(1);
}
$fp = fopen($userfile, "r" );
$image->save(fread($fp, filesize($userfile ) ) );
fclose($fp );
ocicommit($conn );
ocifreestatement($stmt );
} else {
?>
<form method="post" action=" <?php echo $php_self; ?>" enctype="multipart/form-data">
file description:<br>
<input type="text" name="description" size="40">
<input type="hidden" name="max_file_size" value="1000000">
<br>file to upload/store in database:<br>
<input type="file" name="userfile" size="40">
<p><input type="submit" name="submit" value="submit">
</form>
<?php
}
?>
</body>
</html>
//display.php
<?php
/*
purpose:
display an image from picture table
*/
dl("php_oci8.dll");
$conn = ocilogon("scott","tiger");
$stmt = ociparse($conn, "select image, imgtype from picture where picid=23" );
ociexecute($stmt);
@ocifetchinto($stmt, &$result, oci_assoc);
header("content-type: image/".$result[imgtype]);
echo $result[image]->load();
ocilogoff($conn );
?>