【问题标题】:Need help getting this auto-download header to show the right path需要帮助获取此自动下载标题以显示正确的路径
【发布时间】:2011-04-02 11:38:56
【问题描述】:

我有一个可以从播放器播放的 mp3 页面,还有下载歌曲的链接。当用户点击下载链接时,需要打开下载对话框而不是打开媒体播放器,所以建议我使用这个头脚本:

包括/auto-download.php:

<?php
$path = $_GET['path'];
header('Content-Disposition: attachment; filename=' . basename($path));
readfile($path);
?>

然后在我的主页上,链接如下所示:

<a href="includes/auto_download.php?path=Media/Audio/Date/song.mp3">Song Name</a>

我的路径似乎有问题,当我单击链接时,下载框打开,我可以下载一个名称正确的文件,但它不包含任何信息。

为了详细说明我的文件结构,我有这个:

/Patrons(我的 index.php 主页面与我的链接有关

/Patrons/includes(我的 auto-download.php 脚本所在的位置)

/Patrons/Media/Audio/Date/(这是所有歌曲所在的位置)

任何帮助将不胜感激!

【问题讨论】:

    标签: php include header path


    【解决方案1】:

    $path 需要是从 web 根目录到文件的相对路径。没有前导斜杠。如果文件位于 Web 根目录之外,则应使用完整路径 (/home/webuser/song_files/song.mp3)。

    例如$path = 'song_files/'. $_GET['name_of_the_file'];

    您还应该检查文件是否不存在并以错误退出。 这是我在codeigniter中做的一个例子。

    function _begin_download($document_data) {
        basepath = 'uploads/';
        $filepath = basepath . $document_data->filename;
        if (file_exists($filepath)) {
            header("Content-type: " . $document_data->mimetype);
            header("Content-length: " . filesize($filepath));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
            header('Content-disposition: attachment; filename="' . $document_data->filename . '"');
            readfile($filepath);
            exit();
        } else {
            exit('File not found.');
        }
    }
    

    【讨论】:

      【解决方案2】:

      要么将 HTML 代码更改为:

       <a href="includes/auto_download.php?path=../Media/Audio/Date/song.mp3">Song Name</a>
      

      或将 PHP 代码更改为:

       readfile('../'.$path);
      

      【讨论】:

      • 我回来删了这个,因为我也刚想通了——不过你回复的及时! :-D 感谢您抽出宝贵时间!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-13
      • 2019-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多