Jump to content

Force download a file


uc7

Recommended Posts

Hi there,

 

I have the below code. I want to force download a mp3 file. What I want to be able to do, is pass in the song name as an argument to the PHP. For now it's defined in the first line however.

 

When I load this in a browser I get an error:

Parse error: syntax error, unexpected T_STRING in /home/sites/lolipop.jp/users/lolipop.jp-dp39285170/web/dev/gospel/dload/oto/index.php on line 8

 

<?
$file="Oto_AgainISayRejoice_A.mp3";
if ($_GET["getmp3"]) {
header("Content-Type: audio/mpeg");
header('Content-Disposition: attachment; filename='.$file);
header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
   	header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
readfile($file);
exit();
}
?>
<html>
<head>
<title>Mp3 test</title>
<body>

<p><a href="http://www.xxxxxxxx.com/dev/gospel/dload/oto/index.php?getmp3=1">2. Download mp3</a></p>
</body>
</html>
<? //Ends ?>

 

However, if I change the header to the below I can download the file properly. Any suggestions how I can pass the name of the file I want to download in the link? Thanks in advance

 

<?
if ($_GET["getmp3"]) {
header("Content-Type: audio/mpeg");
header('Content-Disposition: attachment; filename="Oto_AgainISayRejoice_A.mp3"');
header('Content-Transfer-Encoding: binary');
readfile("Oto_AgainISayRejoice_A.mp3");
exit();
}
?>

 

Link to comment
https://forums.phpfreaks.com/topic/193988-force-download-a-file/
Share on other sites

Thanks for the feedback! I'm getting somewhere.

 

I still don't understand why hard-coding my download file at the start of the php section gives me a successful download, and pulling it from a variable gives me a empty file with the same name as the php file (index.php).

 

Calling URL is:

<p><a href="http://www.xxxxxxxxx/dev/gospel/dload/oto/index.php?getmp3=1?file=nameOfSong.mp3">Download mp3</a></p>

 

SUCCESSFUL

<?
if ($_GET["getmp3"]) {
$filename = "nameOfSong.mp3";
header("Content-Type: application/force-download");
//header("Content-Disposition: attachment; filename=$filename");
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header("Content-Transfer-Encoding: binary\n");
readfile($filename);
exit();
}
?>

 

UNSUCCESSFUL

<?
if ($_GET["getmp3"]) {
$filename = $_GET["file"];
header("Content-Type: application/force-download");
//header("Content-Disposition: attachment; filename=$filename");
header('Content-Disposition: attachment; filename="'.basename($filename).'"');
header("Content-Transfer-Encoding: binary\n");
readfile($filename);
exit();
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.