Jump to content

Help with scripts to download mp3 files to a client


uc7

Recommended Posts

Hi All,

 

I'm pretty new at PHP so I hope this isn't too easy. I did spent the requisite two hours banging my head on a keyboard before posting it however.

 

My wife has a site, and she has some mp3 songs on it people will download. I'm looking for a script that will download the mp3 file to the client PC.

 

From the manual site I found this:

 

<?php

@ob_end_clean();
// Only allow mp3 files
$allowedFileType = "mp3";

// Set the filename based on the URL's query string
$theFile = $_REQUEST['theFile'];


// Get info about the file
$f = pathinfo($theFile);

// Check the extension against allowed file types
if(strtolower($f['extension']) != strtolower($allowedFileType)) exit;

// Make sure the file exists
if (!file_exists($theFile)) exit;

// Set headers
header("Pragma: public");
header("Expires: Thu, 19 Nov 1981 08:52:00 GMT");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: private");
header("Content-Transfer-Encoding: binary");
header("Content-Type: audio/x-mpeg, audio/x-mpeg-3, audio/mpeg3");
// This line causes the browser's "save as" dialog
header( 'Content-Disposition: attachment; filename="'.$f['basename'].'"' );
// Length required for Internet Explorer
header("Content-Length: ".@urldecode(@filesize($theFile)));

// Open file
if (($f = fopen($theFile, 'rb')) === false) exit;


// Push file
while (!feof($f)) {
   echo fread($f, (1*(1024*1024))); 
   flush(); 
   @ob_flush();
}

// Close file
fclose($f); 
exit;

?>

 

However copying it into the header of a html file, and opening with ~./fileget.php?file=nameOfSong.mp3, where the mp3 file is in the same folder as the php script gets me this error:

 

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

 

Line 43 is "echo fread($f, (1*(1024*1024))); "

Tried changing it from echo fread($f, (1*(1024*1024))); to echo fread($f, (8*(1024*1024))); and the error changed again.

 

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

Link to comment
Share on other sites

The posted code does not produce a php parse error. There must be something present in your actual source file that the copy/paste into the forum post filtered out.

 

I would recommend copy/pasting what you posted here into a completely new file and trying it.

 

Link to comment
Share on other sites

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.