3

I'm storing pdf's on the filesystem and path in database table. I want now based on the ID to open corresponded PDF document in the browser. How can I open and read PDF's? What I have so far is this

require_once("database.php");

if(isset($_GET['upload_id']) && is_numeric($_GET['upload_id']))
{
    $fileFolder='uploads/';

    $sql = "SELECT file FROM documents WHERE upload_id = :id"; 
    $result = $pdo->prepare($sql);
    $result->bindParam(":id", $_GET['upload_id']);
    $result->execute();                 

    $resArray = $result->fetchAll();

    $file = $resArray['file'];

    header('Pragma: public');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Content-Type: application/pdf');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '.filesize($fileFolder.$file));
    @read($fileFolder.$file);

}

When I click on the button and tried to open the pdf I got this message in the Chrome

Failed to load PDF document RELOAD

S.I.
  • 3,250
  • 12
  • 48
  • 77

2 Answers2

1

You can get your file name using fetch(PDO::FETCH_ASSOC); which Return next row as an array indexed by column name

$sql = "SELECT file FROM documents WHERE upload_id = :id"; 
$result = $pdo->prepare($sql);
$result->bindParam(":id", $_GET['upload_id']);
$result->execute();     
$resArray = $result->fetch(PDO::FETCH_ASSOC);
$file = $resArray['file'];// get your file name

By using fetchAll

Pass PDO::FETCH_COLUMN, 0

 $result = $sth->fetchAll(PDO::FETCH_COLUMN, 0);
 $file = $resArray[0];// get your file name
Saty
  • 22,443
  • 7
  • 33
  • 51
  • Thank's for the answer. Now I get the correct path and document but still got this message. `Failed to load PDF document` – S.I. May 06 '16 at 07:57
  • On the `echo $fileFolder.$file` I got `uploads/c970464e4da7eef4bb63c4c510b2ef36.pdf` which is correct – S.I. May 06 '16 at 07:58
  • 1
    Flollow this link http://stackoverflow.com/questions/5670785/chrome-has-failed-to-load-pdf-document-error-message-on-inline-pdfs – Saty May 06 '16 at 07:59
  • 1
    Yes, this was helpful. Now PDF is loaded properly. Thank's for the help! – S.I. May 06 '16 at 08:02
1

try the following:

require_once("database.php");

if(isset($_GET['upload_id']) && is_numeric($_GET['upload_id']))
{
    $fileFolder='uploads/';

    $sql = "SELECT file FROM documents WHERE upload_id = :id"; 
    $result = $pdo->prepare($sql);
    $result->bindParam(":id", $_GET['upload_id']);
    $result->execute();                 

    $resArray = $result->fetchAll();

    $file = $resArray['file'];

    $myFilePath = $fileFolder . $file;

    if (file_exists($myFilePath)) {

        // the file exists, so open it
        header('Pragma: public');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Content-Type: application/pdf');
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: '.filesize($fileFolder.$file));
        @read($fileFolder.$file);

    } else {

        // the file doesn´t exits
        // handle the error if neccessary
        echo "the file doesn´t exist";

    }

}
Michael
  • 663
  • 1
  • 13
  • 28
  • Thank's for the answer but still failed to load and the problem was in headers. With @Saty's comment to the other question I fixed it. – S.I. May 06 '16 at 08:04