This article describes the content of PHP upload/download stored in the database imge type data (image), has a certain reference value, now share to everyone, the need for friends can refer to
Upload scanned pieces (images are stored in data in IMG data type) function Mod_addphoto () {global $row _user; Global $mssql _connection; if ($_files[' myfile ' [' Error '] > 0) {$back = array (' message ' = ' error '), ' Date ' = ' file error '); echo Json_encode ($back); Exit (); } else {//To determine if the table to be uploaded is selected if (Empty ($_post[' upfile ')) {$back = Array (' message ' = ' error ', ' date ' = = ' Upload file cannot be empty '); echo Json_encode ($back); Exit (); }//Get the size of the table, limit the size of the upload table 5 $file _size = $_files[' myfile ' [' Size ']; if ($file _size > 100 * 1024 * 1024) { $back = Array (' message ' = ' ERROR ', ' Date ' = ' file size cannot exceed 5M '); echo Json_encode ($back); Exit (); }//Get file parameters $imgfile = $_files[' myfile ' ]; $name = $imgfile [' name ']; Get the image name $type = $imgfile [' type ']; Get picture Type $size = $imgfile [' Size ']; Get the picture length $tmpfile = $imgfile [' Tmp_name ']; Image upload on the path to the temporary file $file = fopen ($tmpfile, ' RB '); $content = Fread ($filE, $size); $content = "0x". Bin2Hex ($content); Fclose ($file); Limit upload table type $file _type = $_files[' myfile ' [' type ']; $file _type=substr ($file _type, 6); Gets the current time Date_default_timezone_set (' PRC '); $now _time = Date (' y-m-d h:i:s ', Time ()); Save to database $OAV _id = $_get[' oav_id '); $OVA _id = create_id ("Oa_vehicleattach"); Global $mssql _connection; if (! $mssql _connection) {$mssql _connection = OPEN_MSSQLCOnn (); } $sfu _id = $row _user[' sfu_id '); $sql 1 = "INSERT into Oa_vehicleattach (oav_id, Ova_id,ova_name, Ova_date, Ova_type, Ova_size, Ova_data, Ova_user) VALUES (' $OAV _id ', ' $OVA _id ', ' $name ', ' $now _time ', ' $file _type ', $file _size, $content, ' $sfu _id ') "; $sql = Iconv ("Utf-8", "GBK", $sql 1); PHP uses UTF-8 encoding. SQL Server 2008 uses the default GBK encoding to convert the query statement and the data to be inserted into GBK encoding before executing the query statement. $cursor = sqlsrv_query ($mssql _connection, $sql); $AA = sqlsrv_rows_affected ($cursor); if ($AA) {$back = array ( ' Message ' = ' success ', ' Date ' = ' file upload succeeded '); echo Json_encode ($back); Exit (); } else {$back = Array (' Message ' = ' fail ', ' date ' = ' File upload failed ' ); echo Json_encode ($back); Exit (); }}}//Download scan function mod_down_scanning_copy () {$id = $_get[' ova_id '); $id = GETGP (' oav_id ', ' G '); $sql = "Select Ova_id,oav_id,ova_name,ova_data,ova_user,ova_size from Oa_vehicleattach where OVA_id= ' ". $id. "'"; $cursor = Mssqlquery ($sql); $row = Sqlsrv_fetch_array ($cursor, SQLSRV_FETCH_ASSOC); if (Isset ($row [' ova_name '])) {$filename = $row [' Ova_name ']; $filedata = $row [' Ova_data ']; $filesize = $row [' ova_size ']; }//$binaryStr = Base64_decode ($filedata); File_put_contents ($filename, $BINARYSTR); $filePath = ' download/'. $filename; $file = fopen ($filePath, "w");//Open file ready to write Fwrite ($file, $filedata);//write Fclose ($file);//Close/Header ("Content-disposition:attachment;filename=". $filename); Header ("Content-length:". $filesize); ReadFile ($filedata); if (file_exists ($filePath)) {header ("Content-type:application/octet-stream"); Header ("Content-disposition:attachment;filename =". $filename); Header ("Accept-ranges:bytes"); Header ("Content-length:". $filesize); ReadFile ($filePath); }}//sql query function Mssqlquery ($Q) {global $mssql _connection; if (! $mssql _connection) {$mssql _connection = Open_mssqlconn (); } $Q = Iconv ("Utf-8", "GBK", $Q); $cursor = sqlsrv_query ($mssql _connection, $Q); if (! $cursor) {echo "SQL query error. \ n"; Die (Print_r (Sqlsrv_errors (), true)); } return $cursor; }//Get database connection $mssql_connection = '; function Open_mssqlconn () {global $mssql _connection; Global $MSSQL _server; Global $connectionInfo; $MSSQL _server = "desktop-ca04h1q\sqlexpress"; $connectionInfo = Array ("Database" = "Toncenthuajie", "UID" = "User name", "PWD" and "password"); if (! $mssql _connection) {$mssql _link = sqlsrv_connect ($MSSQL _server, $connectionInfo); Sqlsrv_query ("Set NAMES GBK"); if (! $mssql _link) {echo "Could not connect.\n"; Die (Print_r (Sqlsrv_errors (), true)); } return $mssql _link; } else {return $mssql _connection; } }