强制下载 MP3 文件,而不是使用 Quicktime 在浏览器中打开

发布于 2024-12-06 06:19:32 字数 924 浏览 0 评论 0原文

好的,我有一个提供 MP3 格式播客的网站。有一个 .htaccess 将对 MP3 文件的请求路由到下载脚本,如下所示:

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

PHP 下载脚本如下:

$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);

在 PC 和大多数 Mac 浏览器中一切都运行良好。然而,在 Safari 上,Quicktime 会拦截下载。同样的事情也发生在 iPhone 和 iPad 上。由于目标受众可能主要是 Apple 用户,因此我希望“下载”按钮能够做到这一点 - 下载,而不是使用 Quicktime 插件在浏览器中播放,无论设备如何。 “Stream”按钮可以在浏览器中播放文件,但“Download”按钮则不能。

右键单击(或 Cmd+单击)并将文件另存为...是不可接受的。

有没有什么方法可以使用 PHP、HTTP、HTML 等在所有平台上强制下载,除了单击“下载”按钮之外不需要用户执行任何操作?

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 download. 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浪漫之都 2024-12-13 06:19:32

您需要在提供文件时设置 content-disposition HTTP 标头。通过设置此 HTTP 标头,它将指示浏览器提供提示,让用户保存或打开文件。您可以在此处找到更多信息:

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

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

木緿 2024-12-13 06:19:32

您可以结合以下内容将流和下载分为两个单独的文件夹:

“如果要强制下载目录中的所有文件,请在该目录中创建一个 .htaccess 文件,然后将其粘贴到其中:

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

不要粘这将导致您的服务器上的所有文件被下载。”

您可以在此对话中阅读有关此内容的更多信息:

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

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文