Renaming a download file using a header in PHP

I have a link pointing to a music file on my site, now the file name was hashed when it was uploaded, so I want to use the original file name that I saved in my database, I did some research and found this new attribute " loading "for the tag" a ", but it only works in later versions of firefox and chrome, it does not work in ie, and also does not work with the download manager that I use, so I checked online and found out about the headers that I then implemented . Now I got the file name, but the music file continues to be saved as "11.35kb", no matter what 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> 
+7
source share
3 answers

My PHP is a little rusty, but I can think of one thing with your code, without a content length header. Update your code and see if this 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 headers to the browser before we start downloading the actual file. And I also added if (file_exists($filename)) to make sure we have a file to send. I would recommend you put an else clause to give you something that will show you if you don't have a file, as you expect ...

+4
source

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

0
source

Download your mp3 files using curl

Here is a 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> 

Perhaps this will help you.

0
source

All Articles