uc7 Posted March 3, 2010 Share Posted March 3, 2010 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 More sharing options...
teamatomic Posted March 3, 2010 Share Posted March 3, 2010 Whats inside the header() must be contained. Look at the quote containment in your given example that works vs the one that doesn't. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/193988-force-download-a-file/#findComment-1020861 Share on other sites More sharing options...
uc7 Posted March 4, 2010 Author Share Posted March 4, 2010 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(); } ?> Link to comment https://forums.phpfreaks.com/topic/193988-force-download-a-file/#findComment-1021218 Share on other sites More sharing options...
uc7 Posted March 4, 2010 Author Share Posted March 4, 2010 Found it, an error in my URL. Thanks for your help! Link to comment https://forums.phpfreaks.com/topic/193988-force-download-a-file/#findComment-1021265 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.