The Queen
The Queen

Reputation: 55

How can I display PDF file from blob data type from database?

Please help me.

I was trying to save PDF file that has been converted into blob data type in database and it was success. But, I don't know how to retrieve (*retrieve is success) back the data and display it again as PDF file. I have tried everything I found in internet source but none of it success. Some of the internet source code that I tried could enable the PDF file that decode from blob to be saved but the file corrupt.

Hope someone can show me the way. Thanks.

Below is the code to display the blob data type into a PDF ($filepath is from the blob):

<?php
ini_set('session.save_path', '../tmp');
session_start();

$req_id = "";
$refno  = "";
$req_status = "";

if(isset($_SESSION['AUTHORISATION']))
{
    include '../conn.php';

    if($_SESSION['AUTHORISATION'] != "ACCESS GRANTED")
    {
        echo "<script type='text/javascript'>alert('Please login to continue..');window.location.href='../login';</script>";
    }
    else
    {
            if((isset($_POST['request_id'])) && (isset($_POST['req_refno'])) && (isset($_POST['file_no'])))
            {
                $req_id = $_POST['request_id'];
                //echo $req_id;
                $refno  = $_POST['req_refno'];
                //echo $refno ;
                $req_status  = $_POST['file_no'];
                //echo $req_status ;

                if($req_status == 1){
                    // echo $req_id;
                    // echo $req_title;
                    $result = mysqli_query($conn, "Select * from attachment where req_refno = '$refno' and type = 'Summary File';");

                    while($row= mysqli_fetch_array($result)){
                        //get row from table selected
                        $filename = $row['req_attachment_name'];
                        //echo $filename;
                        $filepath = $row['req_attachment_path'];
                        //echo $filepath;

                        //save pdf to computer
                        header("Content-Type: application/pdf");
                        header("Content-Length: ".strlen($filepath));
                        header('Content-Disposition: attachment; filename='.$filename);
                        echo $filepath;

                    }

                }
                else
                {
                    echo "<script type='text/javascript'>alert('Problem to open file!');window.location.href='pending';</script>";
                }
            }
            else
            {
                echo "<script type='text/javascript'>alert('Please select a record first!');window.location.href='pending';</script>";
            }
    }
}
else
{
    echo "<script type='text/javascript'>alert('Please login to continue..');window.location.href='../login';</script>";
}

?>

Upvotes: 1

Views: 376

Answers (1)

Stefan Ziegler
Stefan Ziegler

Reputation: 38

Is $filepath = $row['req_attachment_path']; really the content/data of the pdf file? req_attachment_path seems to be the path only and not the content. lease verify that and check the response of the request. In the browser you can see the response in the developer console and there you can see whether the result is the PDF content or simply a path.

Upvotes: 0

Related Questions