繁体   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