Extracting a PDF file from an MSSQL db

wxdqz

New Member
Problem: Pulling binary data out of an mssql table and giving it to the user as an intact PDF file. So far, I can extract the data and save the file, but adobe complains that the file is damaged and cannot be repaired. However, the actual contents of a working PDF file and the one that is "damaged" is nearly the same. I saw that some one already posted a question similar to this one, but more vague, and there was no answer to it.

Here is the code that I've been toying around with. Notice the one header commented out. If I use just that one, IE crashes. If I use the code in its present form, it sends me the damaged file.

require("includes/backend.inc");

if(!$file)
die("No File Selected");

// Query the db and get the data & type
$rs = new Recordset("SELECT blob, mimetype FROM tblArchive WHERE filename = '" . addslashes($file) . "'", $Conn);

if(($Fields = $rs->MoveNext()) == 0)
die("No File by that name!");

$data = $Fields["blob"];
$type = $Fields["mimetype"];
$size = strlen($data);

//header("Content-Type: $type");

header("Content-Type: application/octet-stream\n");
header("Content-Disposition: attachment; filename=\"$file\"\n");
header("Content-Transfer-Encoding: binary");
header("Content-length: $size");

//send file contents
echo $data;
 
Back
Top