Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Downloads?
#1
What's the best and the most secure way to host files (mp3) on my site and allow people to download them just by clicking?

Code:
<?php

$filename = $_GET['file'];

// 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 found this after some browsing, does it look like it'd work?

I just ended up compressing it. I was hoping I could just have a download link for the .mp3.
Reply
#2
Some browsers will try to stream .mp3 files.
You could always just say "right click , save as" on there.
NEW: SA Admin Toolbar Plugin | View All My Plugins
- Shawn A aka Tablatronix
Reply
#3
I just decided to compress it and have it download that. That was the problem I was having, it kept trying to stream the .mp3 and not download. I also have it hosted on mediafire as a alternative.
Reply
#4
Have you tried to add such directives at the beginning of .htaccess file in your root dir ?
Code:
ForceType application/octet-stream .mp3
or
Code:
<FilesMatch "\.(mp3)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>
Addons: blue business theme, Online Visitors, Notepad
Reply
#5
Will that force it to download and not stream?

If that's the case will that stop my players from playing the .mp3?
Reply
#6
Try it out instead of asking.
Addons: blue business theme, Online Visitors, Notepad
Reply
#7
Quote:Try it out instead of asking.

Smile LOL
Reply
#8
Tried it, shit got fucked all up. Spent hours fixing it. Great advice.
Reply
#9
Hours on fixing a rewrite rule ? Just delete it, and if webserver doesn't reload them automatically wait couple minutes.
Such rules can't break physically anything, unless you set them to do such things.

On webhosts I'm working, all things I develop work fine. But I can't recall you mentioned anything about your settings, so next time have on mind, that no one can see what you see, through your eyes.
Addons: blue business theme, Online Visitors, Notepad
Reply




Users browsing this thread: 1 Guest(s)