br0ken Posted February 20, 2009 Share Posted February 20, 2009 I'm trying to hide an MP3 behind a link so that I can control who downloads the file and how many times. To do this I've created a PHP page that decides whether or not to let the user get the file. If the user is approved, they're given the file. To give them the file I've been using the header function as follows: header("Content-type: audio/mpeg"); header("Content-Disposition: attachment; filename=".basename($mp3link)); header("Pragma: no-cache"); header("Expires: 0"); print $file; My problem is that I don't know what format $file should be in. I've tried using fopen to access the file but this doesn't work. My thoughts are that I might have to read it in in binary format (just a guess) but I'm unsure. Any one have any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/146171-solved-header-send-mp3/ Share on other sites More sharing options...
premiso Posted February 20, 2009 Share Posted February 20, 2009 You should readfile the file instead of printing it. Quote Link to comment https://forums.phpfreaks.com/topic/146171-solved-header-send-mp3/#findComment-767415 Share on other sites More sharing options...
The Little Guy Posted February 20, 2009 Share Posted February 20, 2009 you should also place the MP3 outside of the root, so it cannot be accessed via web browser. Quote Link to comment https://forums.phpfreaks.com/topic/146171-solved-header-send-mp3/#findComment-767416 Share on other sites More sharing options...
angelcool Posted February 20, 2009 Share Posted February 20, 2009 This might interest you a bit: http://www.phpfreaks.com/forums/index.php/topic,238519.msg1111731.html#msg1111731 As The Little Guy said place your file outside your web directory. Quote Link to comment https://forums.phpfreaks.com/topic/146171-solved-header-send-mp3/#findComment-767419 Share on other sites More sharing options...
br0ken Posted February 27, 2009 Author Share Posted February 27, 2009 Thanks for the help on this one everyone. I managed to get it working securely, however, after a small amount of time the file stops downloading. I set the PHP time limit using the following code but the file still stops downloading. set_time_limit(999); Does any one have any ideas why this is happening? I've included my code below. The file being downloaded is roughly 60mb. $file = fopen($link, "r"); if ($file) { $fsize = filesize($link); $buff = fread($file, $fsize); $file = fclose($file); set_time_limit(999); header("Content-type: audio/mpeg"); header("Content-Disposition: attachment; filename=".basename($link)); header("Pragma: no-cache"); header("Expires: 0"); echo $buff; exit; } else { $err = "Unable to open the MP3"; break; } Quote Link to comment https://forums.phpfreaks.com/topic/146171-solved-header-send-mp3/#findComment-772511 Share on other sites More sharing options...
premiso Posted February 27, 2009 Share Posted February 27, 2009 Send the filesize with the header. It should tell the browser how long to stay open. Quote Link to comment https://forums.phpfreaks.com/topic/146171-solved-header-send-mp3/#findComment-772540 Share on other sites More sharing options...
RussellReal Posted February 27, 2009 Share Posted February 27, 2009 Content-Length in regards to what premiso advised.. and set_time_limit(999); you could set that to 0 incase the user is on dialup and is seriously slow lol Quote Link to comment https://forums.phpfreaks.com/topic/146171-solved-header-send-mp3/#findComment-772549 Share on other sites More sharing options...
br0ken Posted March 1, 2009 Author Share Posted March 1, 2009 That worked perfectly. Thanks very much! Quote Link to comment https://forums.phpfreaks.com/topic/146171-solved-header-send-mp3/#findComment-773781 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.