Jump to content

Insert MS Word file into SQL Server


wlee345

Recommended Posts

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
Share on other sites

  • 3 weeks later...
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.