Author Topic: Help with scripts to download mp3 files to a client  (Read 365 times)

0 Members and 1 Guest are viewing this topic.

Offline uc7Topic starter

  • Irregular
  • Posts: 5
    • View Profile
Help with scripts to download mp3 files to a client
« on: March 01, 2010, 08:59:02 AM »
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:

Code: [Select]
<?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
« Last Edit: March 01, 2010, 08:59:38 AM by uc7 »

Offline PFMaBiSmAd

  • Guru
  • 'Insane!'
  • *
  • Posts: 14,588
  • In Coding, Automatic means you write code to do it
    • View Profile
Re: Help with scripts to download mp3 files to a client
« Reply #1 on: March 01, 2010, 09:13:43 AM »
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.
Signature: (not a comment about anything you posted unless specifically indicated)
Debugging step #1: To get past the garbage-out equals garbage-in stage in your code, you must check that the inputs to your code are what you expect.

Programming is just problem solving, but it is done in another language. You must learn enough of the programming language you are using to be able to read and write code.