I'm having trouble with getting a PHP script to pull a file (a PDF file) out of a 
MySQL database. I don't know if the problem is the insertion or the retrieval, but 
I'll try to paste as much code as I can to hopefully reveal to someone more skilled in 
PHP than I what the problem is. The following is part of the code that inserts the 
file into the database:

$file = addslashes(fread(fopen($path . $filename, "r"), filesize($path . $filename)));
$filesize = filesize($path . $filename);
$addquery = "INSERT INTO table (name, idnumber, file, filesize) 
             VALUES ('$name', '$idnumber','$file', '$filesize')";
mysql_db_query($db, $addquery, $mysqlsock);

And then the page that is supposed to retrieve the file and display it again:

if ($id) {
  mysql_pconnect("localhost", $username, $password);
  $query = "SELECT file, filesize FROM table WHERE idnumber = $id";
  $result = mysql_query($query);
  $data = mysql_result($result, 0, "file");
  $size = mysql_result($result, 0, "filesize");
  header("Accept-Ranges: bytes");
  header("Content-type: application/pdf");
  header("Content-length: $size");
  echo $data;

What happens is that loading the page with the above code launches Acrobat Reader, but 
nothing displays on the screen. If I go into Reader and set it not to be browser 
integrated, loading the above page displays the open/save dialog. Choose to open 
launches Acrobat Reader and then it displays a message saying "Unable to open the 
document, a file read error has occurred". The "file" datatype in the table is 
MEDIUMBLOB and the filesize data seems to be accurate. Could someone please offer any 


James Willard

Reply via email to