0

I am getting an error to fetch the pdf file from the database.Below mentioned is my code,please review my code and give me your valuable suggestion.And it's showing output as failed to open the document.Please help me.

<?php
    $server = 'localhost';
    $user = 'root';
    $pass = '';
    $db = 'upload';

// Connect to Database
    $connection = mysql_connect($server, $user, $pass) or die ("Could not connect to server ... \n" . mysql_error ());
    mysql_select_db($db) or die ("Could not connect to database ... \n" . mysql_error ());
    $id = intval($_GET['id']);
    $file=  'SELECT `name`,`size`, `created`,`data` FROM `upload`';
    $result = mysql_query($file);

    if($d = mysql_fetch_array($result))
    {
        $file = $d['name'];
        header('Content-type: application/pdf');
        header("Content-Disposition: inline; name=".$row['name']);
        header('Content-Transfer-Encoding: binary');
        header('Content-Length: ' . size($file));
        header('Accept-Ranges: bytes');
        header("Location: $file");
        @readfile($file);
    }
    else{
        echo'No file with the specified ID exists';
    }
?>
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
sudha
  • 19
  • 3

2 Answers2

0

One issue that I see is the single "=" in your if statement. '=' is for assignment and '==' is for comparison.

Try changing

if($d = mysql_fetch_array($result))

to

if($d == mysql_fetch_array($result)) 

EDIT: However, I don't think that will quite work either. I would try

$d = mysql_fetch_array($result);
if ($d === true) 
infinigrove
  • 489
  • 1
  • 7
  • 16
0
if($result) {
        // Make sure the result is valid
              if($result->num_rows == 1) {

                 $row = mysqli_fetch_assoc($result);
                 header('Content-type: application/pdf');
                 header("Content-Disposition: inline; name=".$row['name']);
                 header('Content-Transfer-Encoding: binary');
                 header("Content-Length: ". $row['size']);
                 header('Accept-Ranges: bytes');
            // Print data
                @readfile($row['data']);
                 echo $row['data'];
               }
                else {
                        echo 'Error! No image exists with that ID.';
                }
sudha
  • 19
  • 3