Jump to content

Archived

This topic is now archived and is closed to further replies.

wlee345

Insert MS Word file into SQL Server

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?

Share this post


Link to post
Share on other sites
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;} ?>

Share this post


Link to post
Share on other sites

×

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.