Jump to content

[SOLVED] Can't get a force download script to work


Wolverine68

Recommended Posts

I posted a thread previously requesting a php script that will force a download of mp3 files.  I did get one but can't get it to work.  Here's the script:

 

<?php

 

$filename = $_GET["http://www.mywebsite.com/audio/audiofile.mp3"];

 

// required for IE, otherwise Content-disposition is ignored

if(ini_get('zlib.output_compression'))

  ini_set('zlib.output_compression', 'Off');

 

// addition by Jorg Weske

$file_extension = strtolower(substr(strrchr($filename,"."),1));

 

if( $filename == "" )

{

  echo "<html><title>eLouai's Download Script</title><body>ERROR: download file NOT SPECIFIED. USE

 

force-download.php?file=filepath</body></html>";

  exit;

} elseif ( ! file_exists( $filename ) )

{

  echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE

 

force-download.php?file=filepath</body></html>";

  exit;

};

switch( $file_extension )

{

  case "pdf": $ctype="application/pdf"; break;

  case "exe": $ctype="application/octet-stream"; break;

  case "zip": $ctype="application/zip"; break;

  case "doc": $ctype="application/msword"; break;

  case "xls": $ctype="application/vnd.ms-excel"; break;

  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;

  case "gif": $ctype="image/gif"; break;

  case "png": $ctype="image/png"; break;

  case "jpeg":

  case "jpg": $ctype="image/jpg"; break;

  default: $ctype="application/force-download";

}

header("Pragma: public"); // required

header("Expires: 0");

header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

header("Cache-Control: private",false); // required for certain browsers

header("Content-Type: $ctype");

// change, added quotes to allow spaces in filenames, by Rajkumar Singh

header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" );

header("Content-Transfer-Encoding: binary");

header("Content-Length: ".filesize($filename));

readfile("$filename");

exit();

 

?>

   

_____________________

 

I get the following error message "ERROR: File not found. USE  force-download.php?file=filepath"

 

As you can see, I defined the exact path in the first line, $filename = $_GET["http://www.mywebsite.com/audio/audiofile.mp3"]; (By the way, the path that I have posted here is not the actual path. Using a generic name for demonstration purposes)

 

I thought I had to leave $filename as it is where it appears in other parts of the script since I already defined $filename in the first line of the script.  So, then I replaced every instance of $filename with the exact path.  I tried using both single and double quotes but it didn't work.

 

Anyone have any suggestions?

I thought "http://www.mywebsite.com/audio/audiofile.mp3" was the absolute path...no?

 

I changed it to $filename = $_GET['/audio/audiofile.mp3'];

 

Still get the error.

 

I realized the .php file, mp3 file, and the webpage that calls the .php file are all located in the audio folder, so I changed it to:

 

$filename = $_GET['audiofile.mp3'];

 

Still get the error.

 

Also, do I need to replace every instance of where $filename appears in the script with the path?  I defined it in the first line so I didn't think I had to.

I've tried all of the following:

 

$filename = "/audio/audiofile.mp3";

 

$filename = "audio/audiofile.mp3";

 

$filename = $_GET["/audio/audiofile.mp3"];

 

$filename = $_GET["audio/audiofile.mp3"];

 

$filename = $_GET['/audio/audiofile.mp3'];

 

Still getting the error.

 

$filename = $_GET['audio/audiofile.mp3'];

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.