7

我有一个指向我网站上的音乐文件的链接,现在文件名在上传时被散列,所以我想使用我存储在数据库中的原始文件名,我做了一些研究,发现了这个新的' “a”标签的“下载”属性,但仅适用于更高版本的 Firefox 和 chrome,它在 ie 中不起作用,也不适用于我使用的下载管理器,所以我在网上查了一下,发现了哪些标题然后我实施了。我现在可以更改文件名,但无论我尝试下载的音乐文件如何,音乐文件都会继续保存为“11.35kb”文件大小。这是我的代码:

if (isset($_REQUEST['download']))
    {
        $download_id = $_REQUEST['download'];

        $db = new MysqliDatabase(ConnectionString);
        $result = array();
        $result = $db->query_one("SELECT TrackID, ma.ArtisteName, FeaturedArtistes, 
                                  mc.Category, TrackName
                                  FROM `musictracks` mt

                                  LEFT JOIN `musiccategories` mc 
                                  ON mt.CategoryID = mc.CategoryID

                                  LEFT JOIN `musicartistes` ma
                                  ON mt.ArtisteID = ma.ArtisteID

                                  WHERE mt.TrackID = '$download_id';");

        $filename = $result->TrackPath;
        $outputfilename = $result->ArtisteName . ' ft. ' . $result->FeaturedArtistes . ' - ' . $result->TrackName . '.mp3';

        header("Content-Type:  audio/mpeg");
        header("Content-Disposition:  attachment; filename=\"" . basename($outputfilename) . "\";" );
        header("Content-Transfer-Encoding:  binary");
        readfile("$filename");
    }

这是下载链接:

<a href="<?php echo 'musicdownload.php?download='. $row->TrackID ?>" ><img src="images/download.png" alt="download" title="download" width="14" height="14" /></a>
4

3 回答 3

4

My PHP is a bit rusty but I can think of one thing with your code, no content length header. Update your code to this and see if that works:

if (isset($_REQUEST['download'])) {
  {
    $download_id = $_REQUEST['download'];

    // ...

    $filename = $result->TrackPath;
    $outputfilename = $result->ArtisteName . ' ft. ' . $result->FeaturedArtistes . ' - ' . $result->TrackName . '.mp3';

    if (file_exists($filename)) {
      header("Content-Type:  audio/mpeg");
      header("Content-Disposition:  attachment; filename=\"" . basename($outputfilename) . "\";" );
      header("Content-Transfer-Encoding:  binary");

      header('Content-Length: ' . filesize($filename));
      ob_clean();
      flush();
      readfile($filename);
      exit;
    }
  }
}

Note that we use flush(); to send the headers to the browser before we start downloading the actual file. And I also added an if (file_exists($filename)) to make sure we have a file to send. I'd recommend you put an else clause there to give you something that will show you if you don't have a file like you expect...

于 2013-06-24T13:32:31.250 回答
0

header("Content-Type: application/force-download"); header("Content-Type:audio/mpeg"); header("Content-Type: application/download");; header("Content-Disposition: attachment;filename=".$file_name);

于 2013-08-05T12:17:04.227 回答
0

请使用 curl 下载您的 mp3 文件

这是供您参考的示例代码

<?php

if(isset($_REQUEST['inputurl']) && $_REQUEST['inputurl']!="") {

$file = $_REQUEST['inputurl']; 

header("Content-type: application/x-file-to-save"); 
header("Content-Disposition: attachment; filename=".basename($file)); 
readfile($file); 

}

?>

<form name="from" method="post" action="">
<input name="inputurl" type="text" id="inputurl"  value="" />
<input type="submit" name="Button1" value="Get File" id="Button1" />
</form>

也许它会帮助你。

于 2013-08-05T12:26:03.553 回答