Home >Backend Development >PHP Tutorial >Store pictures in ORACLE in PHP_PHP tutorial

Store pictures in ORACLE in PHP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 16:54:18999browse

Here I provide you with an example of using PHP to manipulate blob fields. I hope it will be helpful!
This example is to store image files uploaded by users into BLOB.
Suppose there is a table with the following structure:
CREATE TABLE PICTURES (
ID NUMBER,
DESCRIPTION VARCHAR2(100),
PICTURE BLOB
);
Then it is used PHP program code that processes data.
〈?php
//Establish Oracle database connection
$conn = OCILogon($user, $password, $SID);
//Submit SQL statement to Oracle
//Here Two points to note: First, use the EMPTY_BLOB() function. This is an internal function of Oracle that returns a LOB locator. When inserting a LOB, you can only use this method to first generate an empty LOB locator, and then operate on this locator. The EMPTY_BLOB() function is for the BLOB type, and the corresponding one for CLOB is EMPTY_CLOB(). The second is the part after RETURNING, which returns the picture so that PHP's OCI function can handle it.
$stmt = OCIParse($conn,"INSERT INTO PICTURES (id, description, picture)
VALUES (pic_seq.NEXTVAL, '$description', EMPTY_BLOB()) RETURNING picture INTO :PICTURE");
//Generate a descriptor of a local LOB object. Note the second parameter of the function: OCI_D_LOB, which means generating a LOB object. Other possibilities are OCI_D_FILE and OCI_D_ROWID, which correspond to BFILE and ROWID objects respectively.
$lob = OCINewDescriptor($conn, OCI_D_LOB);
//Bind the generated LOB object to the locator returned by the previous SQL statement.
OCIBindByName($stmt, ':PICTURE', &$lob, -1, OCI_B_BLOB);
OCIExecute($stmt);
//Save data into the LOB object. Because the source data here is a file, use the savefile() method of the LOB object directly. Other methods of LOB objects include save() and load(), which are used to save and retrieve data respectively. But the BFILE type has only one method which is save()
if($lob-〉savefile($lob_upload)){
OCICommit($conn);
echo "Upload successful〈br〉";
}else{
echo "Upload failed〈br〉";
}
//Release LOB object
OCIFreeDesc($lob);
OCIFreeStatement($stmt);
OCILogoff( $conn);
?〉

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631759.htmlTechArticleI will provide you with an example of using PHP to manipulate blob fields. I hope it will be helpful! This example is to store image files uploaded by users into BLOB. Suppose there is a table with the following structure...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn