Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Renaming Download File Using Header in PHP

Tags:

html

php

mysql

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>
like image 426
Blank EDjok Avatar asked Jun 24 '13 12:06

Blank EDjok


People also ask

How can we rename a file in PHP?

PHP rename() Function rename("images","pictures"); rename("/test/file1. txt","/home/docs/my_file. txt");

How rename file after upload in PHP?

You can simply change the name of the file by changing the name of the file in the second parameter of move_uploaded_file . $temp = explode(".", $_FILES["file"]["name"]); $newfilename = round(microtime(true)) . '.

What is rename function in PHP?

rename() function in PHP The rename() function renames a file or directory. The function returns TRUE on success or FALSE on failure.


1 Answers

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

like image 152
Jonas Schubert Erlandsson Avatar answered Sep 28 '22 18:09

Jonas Schubert Erlandsson