|
Posted by Jim Michaels on 01/15/06 12:35
It will be in binary in the database. Use mysql_escape_string() to encode
the data and stripslashes() to decode the data.
<?php //viewdoc.php
$q=mysql_query("SELECT doc FROM mytable WHERE doc_id=$_GET[id]", $link);
if ($row=mysql_fetch_array($q)) {
header("Content-Type: application/msword");
header('Content-Disposition: attachment; filename="download.doc"');
echo stripslashes($row['doc']);
}
?>
and to insert data into the table (the stuff at the bottom only updates),
$sql = 'INSERT INTO mytable(doc) VALUES (0x';
$fh = fopen($_FILES['userfile']['tmp_name'],'rb');
while(true) {
$chunk = unpack('H*',fread($fh,512));
$sql .= $chunk['chunk'];
if(strlen($chunk['chunk'])<512) break;
}
fclose($fh);
$sql .= ")";
mysql_query($sql, $link);
wouldn't that unpack be simply unpack('H*',fread($fh,512))) ? chunk would
really mess things up when unpack tries to interpret it wouldn't it?
"monomaniac21" <marcrice20@msn.com> wrote in message
news:1137244241.984444.282530@g49g2000cwa.googlegroups.com...
>
> Robert Lummert wrote:
>> Marc,
>>
>> you can put your binary data into a blob field (say named doc) using
>> straight sql just like:
>>
>> $sql = 'UPDATE mytable SET doc=0x';
>> $fh = fopen($_FILES['userfile']['tmp_name'],'rb');
>> while(true) {
>> $chunk = unpack('H*chunk',fread($fh,512));
>> $sql .= $chunk['chunk'];
>> if(strlen($chunk['chunk'])<512) break;
>> }
>> fclose($fh);
$sql .= " WHERE doc_id=$docid";
>
> Robert how do you then retrieve this data, as a word document that can
> be opened right away?
>
> Kind regards
>
> Marc
>
[Back to original message]
|