Jump to content


Photo

Insert MS Word file into SQL Server


  • Please log in to reply
1 reply to this topic

#1 wlee345

wlee345
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 06 October 2005 - 08:29 PM

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?

#2 Thierry

Thierry
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationNetherlands

Posted 25 October 2005 - 09:54 AM

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?

View Post


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;} ?>

Have you tried....a reset?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users