Hi,
Im working on project right now in webdevelopment. im trying to view a PDF file in a browser which is the filepath was stored in database and the actual file save was in folder inside my webfolder and could retrieve/view by its ID. here's some of my code

<?php 

    if(isset($_POST['search'])){

    $con = new PDO("mysql:host=localhost;dbname=qfms","root","");
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);//setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $fid = $_POST['id'];
    $stmt = $con->prepare("SELECT filename FROM rabbit_tbl WHERE id= '" .$fid. "'");

    $stmt->execute();

    while($row = $stmt->fetch()){
        header('Content-type: application/pdf');
        header("Content-Disposition: inline");
        echo "/pdffiles" .$row['id']. ".pdf");
        readfile('pdffiles/');
    }
}else{
        echo "Enter id";
    }

?>

My issue here, when i run/compile it, it says "Failed to load PDF document". what am i missing here? do you have any suggestions, tutorial link?

Thanks guys.

Member Avatar for diafol

Doesn.t make any sense. You.re pulling one record yet are running a while loop. Why? What.s readfiles supposed to be doing? Reading the directory?

You may read this thread. As @diafol said, your code doesn't make sense. You are expecting ONLY 1 record from your query, so using while loop is unnecessary (or does not make sense).

Thanks @Taywin and @diafol

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.