下载音频/mpeg mime 类型的文件

发布于 2024-12-02 19:06:42 字数 245 浏览 1 评论 0原文

我正在尝试下载具有音频/mpeg mime 类型的远程 mp3 文件,而不是右键单击链接然后另存为。我尝试使用 php 标头修改标头内容类型,然后使用 readfile() 调用该文件。这工作得很好,但由于 readfile() 命令,文件超出了我的服务器带宽。是否有另一种方法可以在不消耗带宽的情况下更改标头?我可以用 javascript 定义浏览器如何处理内容类型吗?有人遇到同样的问题吗?

提前致谢。

I am trying to download remote mp3 files with an audio/mpeg mime type instead of right clicking on the link then saving as. I have tried modifying the header content-type with php headers and then calling the file with readfile(). This worked very well but because of the readfile() command the files came out of my servers bandwidth. is there another way of changing the header without the cost of bandwidth? can i define how the browser handles with content type with javascript? has anyone had the same problem?

Thanks in advance.

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

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

发布评论

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

评论(2

苍风燃霜 2024-12-09 19:06:42

通过使用 mime 类型 audio/mpeg,您可以告诉浏览器“对此文件执行默认操作”。例如,如果您有一个 jpg 文件并将 mime 类型设置为 image/jpeg,则浏览器将读取该 jpg 并将其显示在浏览器窗口中。

解决方案是改用 mime 类型 application/data。这将下载文件,而不会浏览器。

这将是

header("Content-type: application/data");

==更新==

一个更完整的方法

header("Content-type: application/data");
header("Content-Disposition: attachment; filename=$this->filename");
header("Content-Description: PHP Generated Data");
readfile($this->file);

如果你想要一个动态mime类型阅读器你可以使用这个

$type = $this->get_mime_type($this->filename);
header("Content-type: " . $type);

...

private function get_mime_type($filename) {

    $fileext = substr(strrchr($filename, '.'), 1);

    if (empty($fileext)) {
        return (false);
    }

    $regex = "/^([\w\+\-\.\/]+)\s+(\w+\s)*($fileext)/i";
    $lines = file("mime.types", FILE_IGNORE_NEW_LINES);

    foreach ($lines as $line) {
        if (substr($line, 0, 1) == '#') {
            continue; // skip comments
        }

        if (!preg_match($regex, $line, $matches)) {
            continue; // no match to the extension
        }

        return ($matches[1]);
    }
    return ("application/data");  // no match at all, revert to something that will work
}

并且,要获取mime类型列表你可以检查我的实验室版本,保存显示的内容并将其保存到网站根目录下名为 mime.types 的文件中。

http://www.trikks.com/lab/mime.html

玩得开心

By using the mime type audio/mpeg you tell the browser to "do your default action with this file". In example if you have an jpg file and set the mime type to image/jpeg the browser will read the jpg and display it inside the browser window.

The solution is to use the mime type application/data instead. This will download the file leaving the browser out of it.

That would be

header("Content-type: application/data");

== Updated ==

A more complete approach

header("Content-type: application/data");
header("Content-Disposition: attachment; filename=$this->filename");
header("Content-Description: PHP Generated Data");
readfile($this->file);

If you want a dynamic mime type reader you could use this

$type = $this->get_mime_type($this->filename);
header("Content-type: " . $type);

...

private function get_mime_type($filename) {

    $fileext = substr(strrchr($filename, '.'), 1);

    if (empty($fileext)) {
        return (false);
    }

    $regex = "/^([\w\+\-\.\/]+)\s+(\w+\s)*($fileext)/i";
    $lines = file("mime.types", FILE_IGNORE_NEW_LINES);

    foreach ($lines as $line) {
        if (substr($line, 0, 1) == '#') {
            continue; // skip comments
        }

        if (!preg_match($regex, $line, $matches)) {
            continue; // no match to the extension
        }

        return ($matches[1]);
    }
    return ("application/data");  // no match at all, revert to something that will work
}

And, to get a list of mime types you can check my lab version, save the displayed content and save it to a file named mime.types in the root of your website.

http://www.trikks.com/lab/mime.html

Have fun

断舍离 2024-12-09 19:06:42

我认为您需要做的是:

$pathOfAudioFile = '/path/to/my/file.mp3';

header('Content-Type: audio/mpeg');
header('Content-Length: '.filesize($pathOfAudioFile));

// This next line forces a download so you don't have to right click...
header('Content-Disposition: attachment; filename="'.basename($pathOfAudioFile).'"');

readfile($pathOfAudioFile);

使用 Content-Disposition: Attachment... 强制显示下载框,而不必右键单击 ->将目标另存为。

I think what you need to do is this:

$pathOfAudioFile = '/path/to/my/file.mp3';

header('Content-Type: audio/mpeg');
header('Content-Length: '.filesize($pathOfAudioFile));

// This next line forces a download so you don't have to right click...
header('Content-Disposition: attachment; filename="'.basename($pathOfAudioFile).'"');

readfile($pathOfAudioFile);

Using Content-Disposition: attachment... forces a download box to appear instead of having to right click -> save target as.

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