簡體   English   中英

用php下載文件不起作用

[英]Download a file in php not working

我開發了一個從服務器下載MP3文件的代碼。 該代碼在舊服務器上有效,但在新服務器上無效。 當我嘗試下載罰款時,它顯示0個字節

我通過HTTP Post方法發送下載鏈接

www.example.com/download.php?dlink=http://www.example.com/music/sample.mp3

這是我使用的代碼

    ob_start();
    if(isset($_REQUEST['dlink']))
    {
        $file = $_REQUEST['dlink'];
        header('Content-Description: File Transfer');
        header('Content-Type: application/octet-stream');
        header('Content-Disposition: attachment; filename='.basename($file));
        header('Content-Transfer-Encoding: binary');
        header('Expires: 0');
        header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
        header('Pragma: public');
        header('Content-Length: ' . filesize($file));
        ob_clean();
        flush();
        readfile($file);
         exit;
    }
ob_flush();

提前致謝

您不應在dlink參數中傳遞絕對URL,因為您無法從遠程服務器讀取文件。

使用此網址www.example.com/download.php?dlink=music/sample.mp3

假定可執行文件位於根目錄中。

暫無
暫無

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

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