2016-11-05 44 views
0

从数据库中获取pdf文件时出现错误。下面提到的是我的代码,请查看我的代码并给我提供宝贵的建议。并且它显示输出为无法打开该文件。请帮助我。在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'; 
    } 
?> 

回答

0

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

尝试改变

if($d = mysql_fetch_array($result)) 

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

编辑:不过,我不认为这会完全奏效。我会尝试

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

未定义的变量:d在select.php上线14我得到错误像这样请帮助我。 – sudha

+0

你试过我的第二个解决方案吗?这应该分配/定义$ d。 – infinigrove

+0

直接去其他部分.... – sudha

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