2016-04-22 82 views
2

我已經上傳圖像到MySQL數據庫。我正在嘗試下載保存的文件。但我的腳本顯示以下警告:PHP文件不下載

警告:不能更改頭信息 - 已經

發送了頭我也測試:Content-Disposition: attachment。但它仍然不起作用。文件不下載,而是打印文件:

Printed file

這裏是我的代碼:

$query = "SELECT * FROM Upload"; 
$result = mysqli_query ($con, $query) 
    or die ("Couldn't execute SELECT query: ". mysqli_error($con)); 
$nrows = mysqli_num_rows($result); 


echo '<table> 
    <tr> 
     <td>Id</td> 
     <td>Name</td> 
     <td>Type</td> 
     <td>Size (MB)</td> 
    </tr>'; 
    $selfpg = $_SERVER['PHP_SELF']; 
    while($row = mysqli_fetch_assoc($result)){ 
     extract($row); 
     echo "<tr> 
       <td>$Id</td> 
       <td>$Name</td> 
       <td>$Type</td> 
       <td>$SizeMB</td> 
       <td> <a href='$selfpg?id=$Id'> Download </a> </td> 
      </tr>";  
} 
echo '</table>'; 

//---------------------------------------------------------------------- 
if (isset($_GET['id'])){ 
    mysqli_data_seek($result,0); 
    $row = mysqli_fetch_assoc($result); 
    extract($row); 
    header("Content-Type: ". $Type); 
    header("Content-Length: ". $SizeMB); 
    header("Content-Disposition: inline; filename= ". $Name); 
    echo $Content; 
} 
else{ 
    echo "<br>No id received to download a file</br>"; 
} 
+0

想要下載圖片,或將其包含到網頁上? – slax0r

+0

我想下載它。 – Amali

回答

1

你的腳本啓動與echo輸出數據。這意味着已經發送了默認標題,因爲您正在向頁面回覆某些內容,因此您無法重新發送它們。爲了能夠在頁面上回顯某些內容,同時發送文件下載所需的頭文件,您應該使用output buffering