0

There is a pdf file stored in my database as a [BLOB - 143.3 KiB]. It's got a userid of 12. I am trying to call it to the page so that when I click on a button, the pdf populates the webpage.

if (isset($_POST["work"]) && !empty($_POST["work"])) 
 {
   $result = mysql_query("SELECT file FROM ce WHERE userid=12", $c) or die("six");
   $document1=mysql_result($result, 0, 'file'); 
   echo $document1;
 }

 echo '
       <form action="yourcase.php" method="post">
          <input type="hidden" name="work" value="1">
          <input type="image" id="work" src="images/papers.png">
       </form>'; 

Currently, it echos out a page of this: ‰ŒA Â@E÷9Å_ê&&Ói[Ž€°£ ZqŠôúÆŽ@ïåÿ&ŠÆÑ,¢Y[«*ÆX%Ó@/RˆOÝçÇ¡.

Using another post, I was able to call a pdf file from my desktop, but I can't figure out how to do it from the database.

$file = 'sample.pdf';
$filename = 'sample.pdf'; 
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
@readfile($file); 

Thanks to @Fred, I was able to piece together a working solution. This does what I've been trying to do:

if (isset($_POST["work"]) && !empty($_POST["work"])) 
 { 
   $result = mysql_query("SELECT file FROM ce WHERE userid=12", $c) or die("six");
   $document1=mysql_result($result, 0, 'file'); 
   header('Content-type: application/pdf');
   echo $document1;
 }

 echo '
        <form action="fetchdoc.php" method="post">
           <input type="hidden" name="work" value="1">    
           <input type="image" id="work" src="images/papers.png">
        </form>'; 
Afzal Khan
  • 125
  • 2
  • 14
Tim
  • 63
  • 1
  • 1
  • 10
  • Why 2 different file names? `$filename` - `$file` – Funk Forty Niner Aug 10 '13 at 02:29
  • I got it from here http://stackoverflow.com/questions/4679756/show-a-pdf-files-in-users-browser-via-php-perl – Tim Aug 10 '13 at 02:31
  • That's in case you wish to call it "another" name, hence `$filename = 'Custom file name for the.pdf';` which I doubt you want to do, unless you want to email it as an attachment or for downloading under another name. – Funk Forty Niner Aug 10 '13 at 02:32
  • Have a look at this question on SO, including my answer on the same page. It may be of help http://stackoverflow.com/questions/17872470/files-sometimes-download-as-php-instead-of-pdf – Funk Forty Niner Aug 10 '13 at 02:37
  • thanks Fred, but that didn't work for me. – Tim Aug 10 '13 at 02:42
  • You're welcome. I'm still looking for some info for you. Here's another http://stackoverflow.com/questions/9692650/viewing-pdf-files-stored-in-mysql-database-using-php-error see answer on that page. and http://stackoverflow.com/a/5632740/1415724 – Funk Forty Niner Aug 10 '13 at 02:44
  • This looks promising. Read the whole page and the final result is at the end http://forums.devshed.com/php-development-5/retrieve-a-pdf-from-my-database-and-display-it-651075.html – Funk Forty Niner Aug 10 '13 at 02:52
  • Here's another http://www.tim-carter.com/index.php?t=Correctly+Show+PDF+from+MYSQL+Database&Menu=2&SubMenuId=5&ItemId=100 – Funk Forty Niner Aug 10 '13 at 02:56
  • Well, that's it. That's all I could find. I hope some will serve you well, cheers. – Funk Forty Niner Aug 10 '13 at 03:09
  • It's working now, read above, wish I could upvote you, thank you so much saved me grief mate. – Tim Aug 10 '13 at 03:15
  • I'm glad it worked out for you Tim. Well, if you tell me which link, I could post it as an answer. Or I could post the links in an answer. – Funk Forty Niner Aug 10 '13 at 03:16
  • You're very much welcome Tim :) I posted an answer with the links and could very well serve others who may encounter the same or similar problem. Cheers (*Peace*) – Funk Forty Niner Aug 10 '13 at 03:19
  • You can either accept it as answer or upvote it if you wish in order for the question to answered and not remain as "unanswered". Cheers! Glad I could be of help. – Funk Forty Niner Aug 10 '13 at 03:23

3 Answers3

0

Your going to have to create the pdf file and then use header to read that file. If you don't plan on keep the files, you can load them to a temp directory and then delete them when your done. If you echo the blob data from your database, its like opening a .pdf file with notepad.

wesleywmd
  • 605
  • 1
  • 6
  • 20
  • Great. Could you show how to do that. I'm very new and all I can think of is this – Tim Aug 10 '13 at 02:29
0

Thanks to @Fred, I was able to piece together a working solution. This does what I've been trying to do:

if (isset($_POST["work"]) && !empty($_POST["work"])) { 
$result = mysql_query("SELECT file FROM ce WHERE userid=12", $c) or die("six");
$document1=mysql_result($result, 0, 'file'); 
header('Content-type: application/pdf');
echo $document1;}
echo '<form action="fetchdoc.php" method="post"><input type="hidden" name="work" value="1">    
<input type="image" id="work" src="images/papers.png"></form>'; 
Tim
  • 63
  • 1
  • 1
  • 10