I've gotten some of it working but I still get the file corruption I was afraid of. I can get teh data into the DB, but not back out cleanly.
Code below:
to get the contents of the file into the MS SQL database (not MySQL)
function fileopen($file){
$handle = fopen("$file", "rb"); //OPEN THE FILE
$ctype = mime_content_type('$file');
$contents = file_get_contents($handle, filesize($file)); //READ IN THE FILE
fclose($handle); //CLOSE THE FILE
//echo $contents; //DEBUG TO SEE IF WE HAVE THE FILE
//return($contents);
$data = unpack("H*hex", $contents);
$query = "SELECT * FROM files";
$result = mssql_query( $query );
$count = mssql_num_rows( $result);
$id = $count;
$filename = ltrim(strrchr($file,'\\'),'\\');
$submit = "INSERT into files (file_id,name,data,content_type)values" .
"($id,\"$filename\", 0x".$data['hex'].",\"$ctype\")";
//echo $submit;
mssql_query($submit);
}
And to attempt to retreive it and write it temporarily to the FS for download for testing purposes only, users will not be able to download on te live site:
function fileout($sow_id){
$result = mssql_query("select name,data,content_type from files where file_id = '$sow_id'");
$row = mssql_fetch_assoc($result);
$ctype = $row['content_type'];
echo $ctype;
//header("Content-type: $ctype;");
$name = $row['name'];
echo $name; //debug
$data = $row['data'];
if (!file_exists($name)) {
$handle = fopen($name, 'x');
fwrite($handle,$data);
fclose($handle);
}else{
echo "the file $name already exists.";
}
}