在php中从数据库中获取pdf文件

时间:2016-11-05 11:04:38

标签: php

我从数据库中获取pdf文件时收到错误。提到的是我的代码,请查看我的代码并给我你宝贵的建议。并且它显示输出无法打开文档。请注意帮我。

<?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';
    }
?>

2 个答案:

答案 0 :(得分:0)

我看到的一个问题是if语句中的单个“=”。 '='用于分配,'=='用于比较。

尝试更改

if($d = mysql_fetch_array($result))

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

编辑:但是,我认为这也不会有用。我会试试

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

答案 1 :(得分: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.';
                }