Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.