开发者

Force MP3 files to download instead of opening in the browser with Quicktime

开发者 https://www.devze.com 2023-04-07 19:24 出处:网络
OK I have a website that serves podcasts in MP3 format.There is a .htaccess that routes requests for MP3 files to a download script, and that is as follows:

OK I have a website that serves podcasts in MP3 format. There is a .htaccess that routes requests for MP3 files to a download script, and that is as follows:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule  (.*.mp3)$ download.php?file=$1
</IfModule>

The PHP download script is as follows:

$filename = (isset($_REQUEST['file']) ? $_REQUEST['file'] : '');
$filesize = @filesize($filename);
if ($filename == '' || $filesize == 0) exit(0);
header("Content-Type: audio/mpeg");
header("Content-Description: {$filename}");
header("Content-length: {$filesize}");
header("Content-Disposition: attatchment; filename={$filename}");
readfile($filename);

Everything works beautifully on PC, and in most Mac browsers. However, on Safari, Quicktime is intercepting the down开发者_如何学Goload. Same thing is happening on iPhone and iPad. Since the target audience is likely to be mostly Apple users, I would like the "Download" button to do just that -- download, not play in the browser with the Quicktime plugin, regardless of device. The "Stream" button can play the file in the browser, but not the "Download" button.

Doing a Right-click (or Cmd+Click) and Save File As... is not acceptable.

Is there any way to do force download across all platforms with PHP, HTTP, HTML, etc. that doesn't require the user to do anything except click the Download button?


You need to set the content-disposition HTTP header when serving the file. By setting this HTTP header it will instruct the browser to provide a prompt which lets the user save or open the file. You can find more information here:

http://support.microsoft.com/kb/260519


You can divide your streams and downloads in two separate folders in combination with the following:

"If you want to force all files in a directory to download, create a .htaccess file in that directory, then paste this in it:

## force all file types to download if they are in this directory:
ForceType application/octet-stream

Don’t stick this in your root level .htaccess file. It will cause all the files on your server to download."

You can read more about this in this conversation:

https://css-tricks.com/snippets/htaccess/force-files-to-download-not-open-in-browser/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号