Jump to content

[SOLVED] Header() - Send MP3


br0ken

Recommended Posts

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?

 

Link to comment
https://forums.phpfreaks.com/topic/146171-solved-header-send-mp3/
Share on other sites

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;						
}

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.