Store attachments (Word, Excel, PDF) or pictures to your database. (Mysql, sql server, oracle) three versions

xiaoxiao2021-03-06  20

Some people have asked questions about the storage of attachments (Word, Excel, PDF) or pictures, I have been disturbed. The special sample program is organized, including SQL Server, Oracle, MySQL three versions for reference.

1. For SQL Server (the type of the field of the file is TEXT, you must remove the divinity in the php.ini in front of the semicolil in front of Mssql.TextSize, and the value is set bigger. This problem is solved by PHPTeamk brothers)

Save the storage program:

IF ($ upfile! = '') {

$ Psize = filesize ($ upfile);

$ fp = fopen ($ UPFILE, "RB");

$ mssqldoc = base64_encode (FREAD ($ FP, $ PSIZE));

$ upfiletype = start (SUBSTR (Strrchr ($ UPFILE_NAME, "."), 1));

Fclose ($ fp);

MSSQL_CONNECT ("LocalHost", "SA", "mypassword") or

Die ("Unable to Connect To SQL Server);

@msql_select_db ("db") or Die ("Unable to select Database");

$ SQL1 = "SELECT * from Document Where Do_code = $ AR_SLAVECODE";

$ results = mssql_query ($ sql1);

IF ($ row = mssql_fetch_Array ($ result))

{

$ SQL = "Update Document Set Do_file = ('$ mssqldoc'), do_timeadded = getdate (), do_type = '$ upfiletype'

$ SQL. = "Where do_code = $ ar_slavecode";

}

Else

{

$ SQL = "INSERT INTO Document (Do_code, Do_File, Do_Timeadded, Do_Type) VALUES (";

$ SQL. = "$ AR_SLAVECODE, ('$ mssqldoc'), getdate (), '$ upfiletype';

}

MSSQL_QUERY ($ SQL);

Tune the display procedure from the database:

$ SQL = "SELECT Do_FILE, DO_TYPE from Document Where Do_code = '$ ID'"; $ Result = MSSQL_QUERY ($ SQL);

$ row = mssql_fetch_array ($ result);

$ DATA = Base64_Decode ($ row [do_file]);

$ TYPE = STRTOUPPER ($ ROW [DO_TYPE]);

IF ($ TYPE == "DOC")

Header ("Content-Type: Application / Msword");

ELSE IF ($ TY == "XLS")

Header ("Content-Type: Application / X-MSExcel"); Else IF ($ TYPE == "PDF")

Header ("Content-Type: Application / PDF");

Else

Header ("Content-Disposition: attachment);

Echo $ DATA;

2, for MSSQL

The field type is blob. Assuming the name of the file upload field is Picture.

save Picture:

(FREAD ($ Picture, "R"), $ psize)); MySQL_Connect ($ Host, $ Username, $ Password OR) OR

Die ("Unable to Connect To SQL Server); @MYSQL_SELECT_DB ($ DB) OR DIE (" Unable to

Select Database "); mysql_query (" INSERT INTO IMAGES (Image)

Values ​​'($ mysqlpicture') ") or Die (" can't perform query ";} else {echo" you Did Not

UPLOAD ANY PICTURE ";}?>

First, we pass "IF ($ Picture! =" None ")" ""

Check if there is a file being uploaded. Then, use the addslashes () function to avoid data format errors. Finally, connect MySQL,

Select the database and insert the image.

display image :

CONNECT TO SQL Server "); @Mysql_select_db ($ dB) or Die (" Unable to select Database ");

$ results = mysql_query ("Select * from images") or Die ("can't perform query"; while

($ row = mysql_fetch_object ($ result)) {echo "

> Picnum / ">";}?>

The second.php3 file is as follows:

"); $ row = mysql_fetch_Object ($ result); Header (" Content-Type:

Image / gif "); Echo $ ROW-> Image;?> 3, for oracle

The field type is blob.

Save program:

$ conn = Ocilogon ("DB", "Password", "Database");

IF ($ upfile! = 'None)

{

$ upfiletype = start (SUBSTR (Strrchr ($ UPFILE_NAME, "."), 1));

$ SQL = "INSERT INTO COMMONDocument (cd_file, cd_timeadd, cd_condition, cd_version, cd_type) Values ​​(";

$ SQL. = "Empty_blob (), sysdate, '$ code', '$ cd_version', '$ upfiletype') Returning CD_FILE INTO: CD_FILE";

// echo $ sql;

$ blob_body = ocinewdescriptor ($ conn, oci_d_lob);

$ STMT = OCIPARSE ($ conn, $ sql);

OcibindbyName ($ STMT, ": CD_FILE", & $ BLOB_BODY, -1, OCI_B_BLOB);

Ociexecute ($ STMT, OCI_DEFAULT);

$ fp = fopen ($ UPFILE, "R");

$ blob_body-> save (FREAD ($ FP, FileSize));

OCIMMIT ($ conn);

}

Display program:

.....

echo " test file ";

......

----- Test2.php ----

$ conn = Ocilogon ("OA", "SELECT", "ORCL");

$ SQL = "SELECT CD_FILE, CD_TYPE from Commondocument Where CD_CODE = '$ ID' AND CD_CONDITION = '$ Condition'

$ STMT = OCIPARSE ($ conn, $ sql);

OCIEXECUTE ($ STMT);

OCIMMIT ($ conn);

OCIFETCHINTO ($ STMT, $ ROW, OCI_RETURN_LOBS);

OCIFREESTATEMENT ($ STMT);

OCILOGOFF ($ conn);

$ T

-------------------------------------------------- -------------

转载请注明原文地址:https://www.9cbs.com/read-47256.html

New Post(0)