8

i have a link pointing to a music file on my site, now the name of the file was hashed when it was uploaded so i want to use the original filename which i have stored in my database, I did some research and found this new 'download' attribute for the 'a' tag but that only works in later versions of firefox and chrome, it doesn't work in ie and also doesn't work with the download manager i use so i checked online and found out about headers which i then implemented. I now get the filename changed allright but the music file keeps on getting saved as a '11.35kb' filesize no matter the music file i try to download. This is my code:

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");
    }

And this is the download link:

<a href="<?php echo 'musicdownload.php?download='. $row->TrackID ?>" ><img src="images/download.png" alt="download" title="download" width="14" height="14" /></a>
Blank EDjok
  • 732
  • 2
  • 14
  • 33
  • What does the 11.35kb file contain? – Anthony Sterling Jun 24 '13 at 12:25
  • Put some mp3 file on server and try it download directly - maybe there is some problem with server. And check your `$filename` - maybe it is not what you expect. – furas Jun 24 '13 at 12:27
  • It is saved as a music file but when i try playing it it doesn't work. – Blank EDjok Jun 24 '13 at 12:28
  • Open this file in notepad - maybe inside is text/html with error message – furas Jun 24 '13 at 12:29
  • Maybe you need to add fullpath to `$filename`. – furas Jun 24 '13 at 12:30
  • comment the header() calls and look at the output, there are probably errors. also: remove the `Content-Transfer-Encoding` header, it should not be used in this case and might cause unexpected browser behaviour (see here: http://stackoverflow.com/questions/7285372/is-content-transfer-encoding-an-http-header) – x4rf41 Jun 24 '13 at 12:31
  • Okay i opened it in notepad++ and it seems it contains html code instead. – Blank EDjok Jun 24 '13 at 12:32
  • 1
    So read it - change .mp3 to .html and open in browser. Probably there is error message – furas Jun 24 '13 at 12:35
  • Is `$result` an array or object? You first declare it as the former and use it as the latter (what does `query_one` return?). – Evan Mulawski Jun 24 '13 at 12:41
  • Okay thanx alot i opened it in notepad++ and saw the error it was generating, apparently i was having some trouble with my columns so it wasn't returning the data i wanted, rookie mistake :D, Thanx alot. – Blank EDjok Jun 24 '13 at 12:42

3 Answers3

5

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...

0

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

0

Please download your mp3 files using curl

Here is sample code for your reference

<?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>

may be it will help you.

Prashant Parekh
  • 428
  • 1
  • 6
  • 27