Introduction PHP, "PHP: Hypertext Preprocessor" is a multi-purpose scripting language that is widely used for Open Source and can embed HTML. Its syntax is close to C, Java and Perl, easy to learn. The main goal of this language is to allow web developers to quickly write dynamically generated web pages, however, the functionality of PHP is not limited thereto. PHP generally believes that complex programming tasks faster and more efficiently, and is precisely because of its more stable and eliminating the advantages of less resources become the necessary web script design language for the development of B / S structures. Similar to the role of the middleware, ie grammar
Analyze
Execution. Oracle LOB Data Model In B / S (Browser / Server, Browser / Server) application, you need to store more than just simple text messages, but also some pictures and audiovisual materials or long text. For example, develop a document system, a chart, an attachment, or other binary or long text will not be able to use a normal character or other type of data description, which requires the background database to have the ability to store this data. This feature can be implemented using Oracle LOB objects. Oracle LOB is a data type used to store large amounts of binary and text data (a LOB field can store up to 4GB of data). Currently, it is divided into two types: internal LOB and external LOB. The internal LOB stores the data in the form of the database in the form of the database. Thus, many of the internal LOBs can participate in the transaction, or to back up and restore operations like processing normal data. Oracle8i supports three types of internal LOB: BLOB (binary data), CLOB (single-byte character data), NClob (multi-byte national character data). Where the CLOB and NClob types are suitable for storing long text data, the BLOB field is suitable for storing a large number of binary data, such as images, video, audio, etc. Currently, Oracle8i only supports an external LOB type, namely BFile type. In the database, this type only stores location information in the operating system, and the entity of the data exists in the file system of the operating system in the form of an external file. Thus, the data represented by this type is read-only and does not participate in the transaction. This type can help users manage a large number of files accessed by external programs. PHP Oracle 8 Function Analysis PHP has two Oracle function extensions in PHP, where the Oracle8 function allows access to Oracle8 and Oracle7 databases, which use Oracle8 Call-Interface (OCI8) to support the Oracle location marker bind local and global PHP Variables, fully support LOB, FILE and ROWID, allow users to use user-defined variables, namely user database custom object classes. Oracle8 Function Library The function OCIFETCHINTO is used to retrieve a row of data records in an array, and the syntax of the function is described below:
INT OCIFETCHINTO (Array & Result, Int [Mode])
Where parameter MODE can be omitted, the internal value is OCI_NUM. When accessing the Oracle Lob, if you want to return the LOB object, the Mode should be set to OCI_Assoc OCI_Return_lobs. The function OCIBINDBYNAME is used to bind the PHP variable to the Oracle object to establish a data communication between PHP and Oracle, which describes:
Boolean OcibindbyName (int Stmt, String Ph_name, INT & Variable, INT Length) where: Parameter STMT is a string indicator that is parsed by Oracle parsing functions OCIPARSE. The parameter pH_NAME needs binding Oracle returns the variable name; the parameter variable must add & symbols, table PHP variable addresses. The parameter length is the length of the variable. If it is set to -1, use the maximum value of the specified variable variable; the parameter TYPE can be omitted, its value has an OCI_B_File (binary), OCI_B_CFile (text file), OCI_B_Blob), OCI_B_blob ( Several types such as bit dunciation and OCI_B_RowId (RowID). It is worth noting that when using the new data type LOB / ROWID / BFILE in Oracle8, you must execute the OCINEWDEScriptor () function, and you must set the Length parameter to -1. Functions OCINEWDESCRIPTOR is used to initialize new LOB / FILE descriptions. This function syntax is described as:
String OcinewDescriptor (int connections, int [type])
The Type is defined in the TYPE in the OCIBindbyName function. The necessary environmental configuration Use the PHP's Oracle8 function library requires an Oracle8 client library. Before using this extension, please confirm that you have set an Oracle environment variable for Oracle users and Web Daemon users. The environment variables you need to set are listed below:
Oracle_Home #Oracle Installation Path Oracle_sid # Oracle Database ID LD_LIBRARY_PATH #LD Library Path NLS_LANG #Oralce Region (Language) Set ORA_NLS33 # ORA_NLS33 Path
The best way to verify the above variables is correct, the best way is to execute at Oracle users with Nobody, respectively:
# env
Depending on the result of the output, it is judged whether or not the above environment variables are consistent. After setting the environment variable for web server users, you also need to add web server users (Nobody, WWW) to the Oracle group. For detailed descriptions of Oracle8 clients and PHP installation settings, please refer to the relevant technical manual. Application examples are uploaded in PHP and store files in Oracle Lob fields as follows:
// LOB object initialization, get PHP variable pointer $ LOB = OCINEWDESCRIPTOR ($ conn, oci_d_lob); // Add a record to the relevant file log table, Oracle SQL syntax Explains $ STMT = OCIPARSE ($ conn, "Update T_file set filename = '"$ _ Files [' fj1 '] [' Name ']."', Filetype = '". $ _ Files [' f1 '] [' type ']."', Files = EMPTY_BLOB () where fileID = ". $ NEWID. "Returning Files Into: Lob"); // Bind LOB Variable OCIBindByName ($ STMT, ': Lob ", & $ LOB, -1, OCI_B_BLOB; OCIEXECUTE ($ STMT, OCI_DEFAULT); // Execute Statement IF ($ LOB-> Savefile) {// Submit the form submitted to the Oracle Database OCIMMIT ($ conn) by the LOB pointer; // Successfully upload} else {// failed to successfully upload} OcifreeDesc ($ LOB); OCIFREESTATEMENT ($ STMT); // End the application examples extracted and downloaded from the PHP from the database:
$ STMT = OCIPARSE ($ conn, "Select * from t_ file where fileid = ID"); OCIEXECUTE ($ STMT); IF (OCIFETCHINTO ($ STMT, $ RESULT, OCI_Assoc OCI_RETURN_LOBS)) {// Output File Type Information For browser judgment header ("Content-Type:"); // Output file name, the browser prompts whether to open or download the file Header ("Content-Disposition: attachment; filename =". $ Result [filename]); // More than the use of the Header function, please refer to the PHP Manual, HTML file header information, please refer to Internet RFC 2616. // Output the file stream, here, the browser obtains the file content, the prompts that are downloading or directly opening the file Echo $ Result [Files];} Ocifreestatement ($ STMT); // End
The above example is only the key statement of the application and passes verification on the Linux PHP Apache Oracle8i platform, and the reader can improve and supplement according to their own needs. Conclusion This article explores the implementation of PHP to Oracle LOB access, is only one aspects involved in the basic application techniques in the B / S structural system. The author has compared the execution efficiency of both JDBC and PHP-OCI, and the response speed of the JDBC's OAS HTTP Server (HTTP Server Apache2.0) is inferior to the establishment above the Linux aache PHP-OCI platform. Web Server, which is of course linux's excellent performance, but it is undeniable that PHP and OCI's combination is very good. Due to the lack of attempts to use the Oracle Lob and the PHP Oracle8 function library, in the development of the information management system, in order to implement the management of large data files, the functionality of PHP is not utilized, I hope this article is engaged in PHP database. Techniques have helped the programmer who encounters this problem.