How to display .pdf files from mysql using php
Dear Everyone. I can display images from mysql. But when I want to display the pdf that is also stored in my database, I cannot do that. When I use this code, this code shows a small green part of the image, but not the .pdf files. This code works well on the image. But I cannot show the .pdf files. can anyone help me tell me where in my code i should cahnge. And I am asking please for the correct change line that can display the PDF. thanks in advance
<?php
$servername = "localhost";
$username = "root";
$dbname = "db_dat";
$password = "";
$conn = mysqli_connect($servername, $username, $password, $dbname);
$passno="";
$doi="";
$doe="";
$poi="";
$empid='';
$emp_name='';
$desg='';
$comp_add='';
$dob='';
$img='';
if (isset($_POST['pass'])) {
$passno=$_POST['pass'];
}
if (isset($_POST['dateofissue'])) {
$doi=$_POST['dateofissue'];
}
if (isset($_POST['dateofexpiry'])) {
$doe=$_POST['dateofexpiry'];
}
if (isset($_POST['issueplace'])) {
$poi=$_POST['issueplace'];
}
if (isset($_POST['issueplace'])) {
$poi=$_POST['issueplace'];
}
if (isset($_POST['content'])) {
$empid=$_POST['content'];
}
if (isset($_POST['name'])) {
$emp_name=$_POST['name'];
}
if (isset($_POST['designation'])) {
$desg=$_POST['desgination'];
}
if (isset($_POST['companyaddress'])) {
$comp_add=$_POST['companyaddress'];
}
if (isset($_POST['dateofbirth'])) {
$dob=$_POST['dateofbirth'];
}
if (isset($_POST['image'])) {
$img=$_POST['image'];
}
$sql = "SELECT * from upload WHERE EMP_ID='".$empid."'";
$result = mysqli_query($conn,$sql) ;
while($row = mysqli_fetch_array($result)){
$img= '<img src="data:image/jpeg;base64,'.base64_encode( $row['image'] ).'"/>';
$passno=$row['passport_no'];
$doi=$row['dateofissue'];
$doe=$row['dateofexpiry'];
$poi=$row['placeofissue'];
$empid=$row['EMP_ID'];
$emp_name=$row['employee_name'];
$desg=$row['designation'];
$comp_add=$row['company_address'];
$dob=$row['dateofbirth'];
}
?>
<!DOCTYPE html>
<html>
<head>
<body>
<form method="POST" action="" >
<table>
<tr><td>Enter Employee Id</td> <td><input type="text" name="content" id="content"></td></tr>
<tr><td><input type="submit" class="FormSubmit"></td></tr>
<tr><td>Employee Name</td> <td> <?php echo $emp_name; ?> </td></tr>
<tr><td>Employee desgination</td> <td> <?php echo $desg; ?> </td></tr>
<tr><td>Company Address</td> <td> <?php echo $comp_add; ?> </td></tr>
<tr><td>DOB.</td> <td> <?php echo $dob; ?> </td></tr>
<tr><td>Date of Issue</td> <td> <?php echo $doi; ?> </td></tr>
<tr><td>Date of Expiry</td> <td> <?php echo $doe; ?> </td></tr>
<tr><td>Place of Issue</td> <td> <?php echo $poi; ?> </td></tr>
<tr><td>Passport Image</td> <td> <?php echo $img; ?> </td></tr>
<div class="space"></div>
<div id="flash"></div>
<div id="show"></div>
</div>
</table>
</form>
</body>
</head>
</html>
source to share
This is because you are adding an image instead of a pdf ... there is no c tag <pdf
as we have for an image, so create a clickable link. There is something different between PDF and image.
Your clickable link will be generated
$img="<a href='"$row['image']."'>Download </a> ";
Another way is to use iframes
where will you create iframe and open pdf in iframe
echo" <iframe src='"$row['image']."'></iframe>";
row['image']
must have a full path like
http://hostname.com/uploads/yourPDFfile.pdf
source to share
To display the pdf file, you need to set the appropriate headers for the file display as well. Try to set these headers and replace $ filename with your file path.
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
source to share