2016-11-05 92 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.'; 
       }