wlee345 Posted October 6, 2005 Share Posted October 6, 2005 I try to use the following code to upload a Word document to SQL Server's image column "Document" which can store binary file. define("MAX_SIZE",3000000); $handle = fopen($uploadwordfile, "rb") or die( "Can't open this Word file!" ); $data = base64_encode(fread($handle, MAX_SIZE)); $sql_loadfile = "insert into Media (number, Document) values (1,'".$data."')"; $condata->Execute($sql_loadfile) or die($condata->ErrorMsg()); fclose($handle); $uploadwordfile is the local address of the document file. I try a 3-page text file which is OK but doesn't work for a one-word Word file. It look like the encoded $data is truncated in $sql_loadfile. Anyone can help? Link to comment https://forums.phpfreaks.com/topic/2625-insert-ms-word-file-into-sql-server/ Share on other sites More sharing options...
Thierry Posted October 25, 2005 Share Posted October 25, 2005 I try to use the following code to upload a Word document to SQL Server's image column "Document" which can store binary file. define("MAX_SIZE",3000000); $handle = fopen($uploadwordfile, "rb") or die( "Can't open this Word file!" ); $data = base64_encode(fread($handle, MAX_SIZE)); $sql_loadfile = "insert into Media (number, Document) values (1,'".$data."')"; $condata->Execute($sql_loadfile) or die($condata->ErrorMsg()); fclose($handle); $uploadwordfile is the local address of the document file. I try a 3-page text file which is OK but doesn't work for a one-word Word file. It look like the encoded $data is truncated in $sql_loadfile. Anyone can help? 303631[/snapback] Here, I use this to upload to a MSSQL text field, can hold 3gb //--------------------------------------------------------------file---------- $fileNameTemporaryS = $file; $fileNameClientS = $file_name; $fileSizeS = $file_size; $fileTypeS = $file_type; $fileNameClientS = str_replace(' ', '_', $fileNameClientS); // if ($file == "") { echo ""; } else { // $fd = fopen ($fileNameTemporaryS, 'r'); $sizeB = filesize ($fileNameTemporaryS); $contB = fread ($fd, $size); fclose ($fd); $data = base64_encode($cont); $fileencoded = chunk_split($data,20,'\'.\''); } //----------------------------------------------------------------------------------- Then simply get a query to upload $data to your text field, you can also upload filesize, name and type information This will upload any file, any format, it will Base64_encode it, and then put all the text in the field This will download it again: list( $data, $filename, $filetype, $filesize) = mssql_fetch_array( $result ); chunk_split($data,20,'\'.\''); if ($filesize == "0"){echo"This file doesnt exist. <a href=\"javascript:history.go(-1)\">Back</a>";} else{ header( "Content-length: $filesize" ); header( "Content-type: $filetype" ); header( "Content-Disposition: attachment; name=$filename" ); echo base64_decode($data); } mssql_close( $link ); exit;} ?> Link to comment https://forums.phpfreaks.com/topic/2625-insert-ms-word-file-into-sql-server/#findComment-9100 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.