簡體   English   中英

無法使用PHP強制下載文件

[英]Unable to force download file with PHP

我需要創建一個PHP頁面,該頁面會自動開始下載文件(我不想公開Download鏈接)。 我已經在網絡上嘗試了幾個示例,但是所有示例最終都將在瀏覽器中使用不正確的內容類型打開文件。 例如:

 <?php
// We'll be outputting a ZIP
header("Content-type: application/zip");

// Use Content-Disposition to force a save dialog.
// The file will be called "downloaded.zip"
header("Content-Disposition: attachment; filename=downloaded.zip");

readfile('downloaded.zip');
?>

當我執行此頁面時,瀏覽器上的輸出是: 在此處輸入圖片說明 在嘗試了本示例的所有可能變體之后,我的想法是問題出在我的托管環境上。 我應該檢查哪個變量,或者可能要求對提供程序啟用它?
謝謝!

嘗試使用phpinfo()打印以下變量:SERVER [“ HTTP_ACCEPT_ENCODING”]。 我的懷疑是您不允許使用zip,例如,可能與“ gzip”不同。

這是我以前使用的代碼:

                        header ("Content-Type: application");
                        header ("Content-Disposition: attachment; filename=\"$file\"");
                        header ("Content-Length: " . filesize ('./FILES/' . $file));
                        header ("Cache-Control: no-cache");
                        header ("Pragma: no-cache");
                        readfile ('./FILES/' . $file);
只需修改內容類型標頭ofc。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM