Viewing PDF files stored in MySQL database using PHP error-ThrowExceptions

Exception or error:

I am creating a button, that on click it views the PDF uploaded in the MySQL database in new window
here’s my code:

Button:

<td><a href='view.php?id={$row['id']}'>View</a></td>

view.php:

<?php

header('Content-type: application/pdf');
echo $pdf_from_database;  

the code seems to work and newtab is started, adope start loading, and then I get an error:

file does not begin with “%pdf-”

Anyone knows how to solve the problem?

How to solve:

you are missing some links here.

  1. first the pdf files should be stored somewhere on a server.
  2. what you store in the database is information relative to the pdf file such as a url link to it.

so lets say you have a page http://www.mypage.com and your pdf files are store in http://www.mypage.com/pdf/

you have a pdf file stored in there called file_DBid_83.pdf

when you make a mysql query to open that file, your query returns $row[‘id’] which is 83 for example

so to open the file you go

header('Content-type: application/pdf');
echo "/pdf/file_DBid_" . $row['id'] . ".pdf";

Leave a Reply

Your email address will not be published. Required fields are marked *